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 8265561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:51:05+00:00 2026-06-08T04:51:05+00:00

I am new to ggplot2 and it’s been wonderful, but I’m having difficulty with

  • 0

I am new to ggplot2 and it’s been wonderful, but I’m having difficulty with one thing.

I have plotted a number of time series that span a year. The X axis is derived from a variable of class Date. I have faceted the plot so that I have 7 time series in a column with independent y axes. The whole point of this graphic is to compare the correlation of each facet with the top facet.

The last thing I would like to do is add text (the estimated pearson correlation between each facet and the first) to the plot in the upper right hand corner of each facet.

This is proving to be extremely difficult because geom_text()requires x and y coordinates for each bit of text. How do I specify coordinates when the X axis is dates and the Y axis is different for each facet? Here’s some sample data and the code I have so far so you can reproduce what I have so far:

library(ggplot2)

date <- rep(as.Date(1:365,origin='2011-1-1'),7)
location <- factor(rep(1:7,365))
product <- rep(letters[1:7], each=365)
value <- c(sample(1:10, size=365, replace=T),sample(1:3, size=365, replace=T),
           sample(10:100, size=365, replace=T), sample(1:50, size=365, replace=T),
           sample(1:20, size=365, replace=T),sample(50:100, size=365, replace=T),
           sample(1:100, size=365, replace=T))
dat<-data.frame(date,location,product,value)

qplot(date, value, data=dat, geom="line", color=location, group=location, 
      main='Time Series Comparison', xlab='Month (2011)',ylab='Value') + 
        facet_grid(product ~ ., scale = "free_y")
  • 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-08T04:51:06+00:00Added an answer on June 8, 2026 at 4:51 am

    This isn’t the neatest code, but I think it’s somewhat like what you’re after:

    library(plyr)
    
    corr_dat<-ddply(dat, .(product), summarise, value=value)
    corr.df<-unstack(corr_dat, value~product)
    
    corr_plot <- data.frame(date=max(dat$date),
                            label=paste0("rho==",round(cor(corr.df)[,1], 2)), 
                            ddply(dat, .(product), summarise, 
                              value=(min(value)+max(value))/2))
    
    ggplot(dat, aes(x=date, y=value, color=location, group=location)) + 
      geom_line()+
      facet_grid(product ~ ., scale = "free_y")+
      geom_text(data=corr_plot, aes(x=date, y=value, label=label), 
                colour="black", inherit.aes=FALSE, parse=TRUE)
    

    Plot with expressions

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

Sidebar

Related Questions

New to Applets, I have never dealt with having to export the resources to
New to javascript/jquery and having a hard time with using this or $(this) to
New to SO and javascript. I have been playing with JQuery for a couple
I have been struggling with this for a while. I am new to working
FYI: I'm fairly new to ggplot2 and ggmap so I apologize for the sloppy
New to PHP and MySQL, have heard amazing things about this website from Leo
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I
I am new to ggplot and I have some questions. I hope someone can
I'm fairly new to ggplot. I have made a bumpplot using code posted below.
NEW EDIT: I have narrowed my problem to this - i have a view

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.