0

我有一个格式为的数据框(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

然后我使用meltfromreshape2

p <- ggplot(plot_Data, aes(x=Date, y=Activity, col=Isotope)) + geom_point()+ theme_bw() 在此处输入图像描述

但是,我还想显示一个 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')

但是,这是使用原始帧 pre_melted 数据帧。

当我尝试时,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

如何在原始图像上叠加色带?

这是可以重建剩余数据的原始数据帧 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")

谢谢

4

2 回答 2

4

首先,您发布的数据没有名为minand的列max,但是我找到了相关的列并更改了名称。然后,您可以使用这样的聚合为功能区制作第二个 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)

在此处输入图像描述

这是你所期待的吗?

于 2013-03-22T14:16:12.917 回答
1

问题是(在您取消映射这两种美学之后)ggplot您的列名minmax. 这些恰好也是函数。只需将它们重命名为yminand ymax

我应该补充一点,我不完全确定为什么 ggplot 会被您的完整数据上的列名弄糊涂,但不是当我只是复制+粘贴您的缩写数据时(尽管在后一种情况下,结果图有点荒谬)。但是偶尔奇怪的参数解析问题是你在 ggplot2-land 中必须忍受的事情。

于 2013-03-22T14:35:02.400 回答