将文本添加到ggplot2中的刻面图上,日期位于X轴上

时间:2021-09-19 14:56:12

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

我是ggplot2的新手,这很精彩,但我有一件事情有困难。

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.

我绘制了一系列跨越一年的时间序列。 X轴派生自Date类的变量。我有一个方面,因此我在一个具有独立y轴的列中有7个时间序列。该图的重点是比较每个面与顶面的相关性。

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:

这被证明是非常困难的,因为geom_text()需要每个文本位的x和y坐标。当X轴是日期并且每个面的Y轴不同时,如何指定坐标?这里是一些示例数据和我到目前为止的代码,所以你可以重现我到目前为止:

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


10  

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)

将文本添加到ggplot2中的刻面图上,日期位于X轴上

#1


10  

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)

将文本添加到ggplot2中的刻面图上,日期位于X轴上