将geom_ribbon添加到reshape2数据框

时间:2013-03-22 13:57:49

标签: r ggplot2

我有一个格式为

的数据框(df)
  Date          Cs.137 In.111 I.123  I.131     X99m.Tc min    max
1 2013-01-03    8.2    6.49   6.97   10.5      15      8.294  7.87930
2 2013-01-04    8.1    6.47   6.94   10.5      15      8.293  7.87835
3 2013-01-07    8.1    6.48   6.96   10.5      15      8.292  7.87740
4 2013-01-08    8.1    6.43   6.91   10.5      15      8.291  7.87645
5 2013-01-09    8.1    6.49   6.92   10.5      15      8.291  7.87645
6 2013-01-10    8.1    6.51   6.95   10.5      15      8.290  7.87550

我想在几个日期绘制各种同位素的测量活动作为散点图。为此,我重塑了数据

plot_Data <- melt(df[,c(1,2:6)], id="Date", value.name="Activity", variable.name="Isotope")

  Date        Isotope     Activity
1 2013-01-03  Cs.137      8.2
2 2013-01-04  Cs.137      8.1
3 2013-01-07  Cs.137      8.1
4 2013-01-08  Cs.137      8.1
5 2013-01-09  Cs.137      8.1
6 2013-01-10  Cs.137      8.1

然后我使用melt

中的reshape2来绘制此图

p <- ggplot(plot_Data, aes(x=Date, y=Activity, col=Isotope)) + geom_point()+ theme_bw() enter image description here

然而,我还想展示一个geom_ribbon来突出显示Cs-137活动的允许范围(由原始数据框中的min和max给出)。我可以使用

创建这样的功能区

m <- ggplot(df) + geom_ribbon(data=df, aes(x=Date,ymin=min, ymax = max), alpha = 0.2, fill='blue')

但是,这是使用原始帧预先熔化的数据帧。

当我尝试p + geom_ribbon(data=df, aes(x=Date,ymin=min, ymax = max), alpha = 0.2, fill='blue')时,收到错误消息Error in eval(expr, envir, enclos) : object 'Activity' not found

如何在原始图像上叠加功能区?

这是原始data_frame,可以从中重建剩余数据 structure(list(Date = structure(c(15708, 15709, 15712, 15713, 15714, 15715, 15716, 15719, 15720, 15721, 15722, 15723, 15726, 15727, 15729, 15730, 15733, 15734, 15735, 15736, 15740, 15741, 15743, 15747, 15748, 15749, 15750, 15751, 15754, 15755, 15756, 15757, 15758, 15761, 15762, 15764, 15765, 15768, 15769, 15770, 15771, 15772), class = "Date"), Cs.137 = c(8.2, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.2, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8, 8.2, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1), In.111 = c(6.49, 6.47, 6.48, 6.43, 6.49, 6.51, 6.5, 6.47, 6.48, 6.4, 6.48, 6.48, 6.48, 6.49, 6.49, 6.47, 6.48, 6.48, 6.5, 6.47, 6.49, 6.55, 6.46, 6.49, 6.48, 6.48, 6.46, 6.48, 6.49, 6.44, 6.49, 6.46, 6.45, 6.46, 6.46, 6.43, 6.49, 6.47, 6.45, 6.43, 6.44, 6.44), I.123 = c(6.97, 6.94, 6.96, 6.91, 6.92, 6.95, 6.93, 6.92, 6.93, 7, 6.97, 6.96, 6.96, 6.94, 6.98, 6.97, 6.95, 6.95, 6.94, 6.96, 6.97, 7.01, 6.92, 7, 6.98, 6.97, 6.91, 6.99, 6.95, 6.88, 6.96, 6.91, 6.91, 6.93, 6.94, 6.94, 6.97, 6.93, 6.93, 6.93, 6.96, 6.94), I.131 = c(10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.8, 10.5, 10.6, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.6, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.4, 10.5, 10.4, 10.5), X99m.Tc = c(15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15.1, 15, 15, 15.1, 15, 15, 15, 15, 15.1, 15, 15.1, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 14.9, 15, 15, 15, 15, 15, 15), Exp_Act = c(8.294, 8.293, 8.292, 8.291, 8.291, 8.29, 8.29, 8.288, 8.288, 8.287, 8.287, 8.286, 8.285, 8.284, 8.283, 8.283, 8.281, 8.28, 8.28, 8.279, 8.277, 8.277, 8.276, 8.274, 8.273, 8.273, 8.272, 8.272, 8.27, 8.27, 8.269, 8.269, 8.268, 8.266, 8.266, 8.265, 8.264, 8.263, 8.262, 8.262, 8.261, 8.261), Exp_Act_0.95 = c(7.8793, 7.87835, 7.8774, 7.87645, 7.87645, 7.8755, 7.8755, 7.8736, 7.8736, 7.87265, 7.87265, 7.8717, 7.87075, 7.8698, 7.86885, 7.86885, 7.86695, 7.866, 7.866, 7.86505, 7.86315, 7.86315, 7.8622, 7.8603, 7.85935, 7.85935, 7.8584, 7.8584, 7.8565, 7.8565, 7.85555, 7.85555, 7.8546, 7.8527, 7.8527, 7.85175, 7.8508, 7.84985, 7.8489, 7.8489, 7.84795, 7.84795), Exp_Act_1.05 = c(8.7087, 8.70765, 8.7066, 8.70555, 8.70555, 8.7045, 8.7045, 8.7024, 8.7024, 8.70135, 8.70135, 8.7003, 8.69925, 8.6982, 8.69715, 8.69715, 8.69505, 8.694, 8.694, 8.69295, 8.69085, 8.69085, 8.6898, 8.6877, 8.68665, 8.68665, 8.6856, 8.6856, 8.6835, 8.6835, 8.68245, 8.68245, 8.6814, 8.6793, 8.6793, 8.67825, 8.6772, 8.67615, 8.6751, 8.6751, 8.67405, 8.67405)), row.names = c(NA, -42L), .Names = c("Date", "Cs.137", "In.111", "I.123", "I.131", "X99m.Tc", "Exp_Act", "Exp_Act_0.95", "Exp_Act_1.05"), class = "data.frame")

由于

2 个答案:

答案 0 :(得分:4)

首先,您发布的数据没有名为minmax的列,但我找到了相关列并更改了名称。然后,您可以使用像这样的聚合物为色带制作第二个df ...

mydf <- aggregate( cbind(min , max) ~ Date, data = df , FUN = mean )

然后您可以使用它来绘制数据......

p <- ggplot( NULL )+
  geom_point( data = plot_Data , aes( x = Date , y = value , col = variable ) )+
  geom_ribbon( data = mydf , aes( x = Date , ymin = min , ymax = max ), alpha = 0.2 , fill='blue' )+
  theme_bw()

print(p)

enter image description here

这是你期待的吗?

答案 1 :(得分:1)

问题是(在您解开这两种美学之后)ggplot被您的列名minmax搞糊涂了。那些恰好也是功能。只需将其重命名为yminymax

我应该补充一点,我不完全确定为什么ggplot会被你的完整数据上的列名混淆,但是当我只是复制+粘贴你的缩写数据时(尽管在后一种情况下得到的情节是无意义的)。但是,在ggplot2-land中,你需要解决的问题就是解决问题。