将文本添加到ggplot2中具有X轴日期的分面图 [英] Add text to a faceted plot in ggplot2 with dates on X axis

查看:131
本文介绍了将文本添加到ggplot2中具有X轴日期的分面图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ggplot2的新手,这很棒,但我遇到了一件难事。

我绘制了一系列跨越一年的时间序列。 X轴来自类 Date 的变量。我已经刻画了这个情节,以便在具有独立Y轴的列中有7个时间序列。该图形的全部要点是比较每个面与顶面的相关性。

我想要做的最后一件事就是将文本(每个小平面和第一个小平面之间的估计皮尔逊相关性)添加到每个小平面右上角的绘图。

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

  library(ggplot2)
$ b $ date < - rep(asDate(1:365,origin ='2011-1-1'),7)
location < - factor(rep(1:7,365) )
product< - rep(字母[1:7],每个= 365)
值<-c(sample(1:10,size = 365,replace = T),sample :3,size = 365,replace = T),
sample(10:100,size = 365,replace = T),sample(1:50,size = 365,replace = T),
样本(1:20,size = 365,replace = T),样本(50:100,size = 365,replace = T),
样本(1:100,size = 365,replace = T))
dat <-data.frame(日期,位置,产品,值)

qplot(日期,值,数据= dat,geom =line,color = location,group = location,
main ='时间序列比较',xlab ='Month(2011)',ylab ='Value')+
facet_grid(product〜。,scale =free_y)


解决方案

这不是最好的代码,但我很瘦k它有点像你以后的样子:

  library(plyr)

corr_dat< -ddply (dat,。(product),summary,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),总结,
值=(最小(价值)+最大(价值))/ 2))

ggplot(dat,aes(x = date,y = value,color = location,group =数据=位置))+
geom_line()+
facet_grid(product_。,scale =free_y)+
geom_text(data = corr_plot,aes(x = date,y = value, label = label),
color =black,inherit.aes = FALSE,parse = TRUE)


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")

解决方案

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轴日期的分面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆