Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8269883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:22:42+00:00 2026-06-08T06:22:42+00:00

When using a facet_grid in ggplot2 I would like to be able to have

  • 0

When using a facet_grid in ggplot2 I would like to be able to have value of the correlation for the subsetted data for each grid cell in the top right corner of the specific plot.

e.g. if running:

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(vs ~ am, margins=TRUE)

I would like to see the value for correlation for each of the 9 plots in the grid somewhere. In this specific case from the example, I would expect each to be close to -0.9 or so from visual inspection.

Or perhaps an output table to go with the plot that gives the correlation values for each of the cells in the table matching up with the facet_grid…(this is less desirable but also an option).

Ideally I would like to extend this to any other function I choose that so that it can use either or both of the two variables plotted to calculate statistics.

Is this possible?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:22:44+00:00Added an answer on June 8, 2026 at 6:22 am

    Winston Chang suggested an answer on the ggplot2 group…this is what he said…its not a bad answer…

    You could do something like this:

    p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
    
    # Calculate correlation for each group
    cors <- ddply(mtcars, c("vs", "am"), summarise, cor = round(cor(mpg, wt), 2))
    
    p + facet_grid(vs ~ am) +
    geom_text(data=cors, aes(label=paste("r=", cor, sep="")), x=30, y=4)
    

    I don’t think it’s possible to make this come out correctly with margins=TRUE, though. If you want the margins, you may need to preprocess your data to add an ALL value for each faceting variable.

    -Winston

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm plotting a group of curves, using facet in ggplot2. I'd like to have
I'm using the plotmatrix function in ggplot2 (ggplot2_0.8.8) and would like to override the
I would like to make stat_smooth to fit a line using only the data
I am using ggplot2 to plot a figure that contains nine facets. Each facet
I have a grid view with a nested text box in it. I would
I was looking for a way to plot using facet_grid in ggplot2 that only
I am having trouble plotting sorted values inside each facet using ggplot2. Here is
I have a grid that binds a number of child data objects to columns
I have a problem with using facet. I need autocomplete and for this I
I'm Using rich faces 4 I'm using input number slider <rich:inputNumberSlider value=10 width=500 minValue=1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.