首页 文章

geom_ribbon的扩展范围

提问于
浏览
0

我有geom_ribbon的问题,并有其限制 . 我有一个这样的数据表(长度和Perf是平均值,Length.sd和Perf.sd,标准偏差)

Ex <- data.frame(Temperature = as.factor(c("16", "16", "16", "16", "16")),
                Level = Level = as.factor(c(0, 1, 2, 3, 4)), Length = c(1.623333, 2.161143, 2.924545, 3.895429, 5.068788),
                Length.sd = c(0.1578897, 0.1161331, 0.1850691, 0.1691039, 0.1937743),Perf = c(0.6693438, 0.5292980, 0.1979450, 0.1034701, 0.0827987),
                Perf.sd = c(0.04479068, 0.07299800, 0.02513500, 0.00876010, 0.00679870))

然后,我构建了我的图形,我使用geom点作为平均值,geom_errorbar表示垂直误差条,geom_errorbarh表示垂直误差条,stat_function用于绘制数据集的线性回归 . 到现在为止,一切都很好

ggplot(data=Ex, aes(x=log10(Length), y=log10(Perf), color=Temperature))+ 
  geom_point(aes(shape=Level), size = 2)+
  geom_errorbarh(aes(xmax = log10(Ex$Length + Ex$Length.sd), xmin = log10(Ex$Length - Ex$Length.sd), height = 0.01))+
  geom_errorbar(aes(ymax = log10(Ex$Perf + Ex$Perf.sd), ymin = log10(Ex$Perf - Ex$Perf.sd), width = 0.01))+
  scale_color_manual(values=c("blue"))+
  scale_shape_manual(values=c(7, 15, 17, 19, 6))+
  stat_function(fun=function(x){0.2746- 1.9945*x}, colour="red", size=1) +
  theme_bw() + 
  scale_x_continuous("Length") + 
  scale_y_continuous("Perf")

enter image description here

然后,geom_ribbon .

ggplot(data=Ex, aes(x=log10(Length), y=log10(Perf), color=Temperature))+ 
  geom_point(aes(shape=Level), size = 2)+
  geom_errorbarh(aes(xmax = log10(Ex$Length + Ex$Length.sd), xmin = log10(Ex$Length - Ex$Length.sd), height = 0.01))+
  geom_errorbar(aes(ymax = log10(Ex$Perf + Ex$Perf.sd), ymin = log10(Ex$Perf - Ex$Perf.sd), width = 0.01))+
  scale_color_manual(values=c("blue"))+
  scale_shape_manual(values=c(7, 15, 17, 19, 6))+
  stat_function(fun=function(x){0.2746- 1.9945*x}, colour="red", size=1) +
  geom_ribbon(aes(ymax= 0.3186 -2.1155*x+0.1197*x^2 , 
                  ymin= 0.2338  -1.8895*x-0.1033*x^2 ), fill="red", colour= NA, alpha=.2) + #CI 16
  theme_bw() + 
  scale_x_continuous("Length") + 
  scale_y_continuous("Perf")

首先,我有一条错误消息,就好像它没有在aes中看到x = log10(长度)

错误:美学必须是长度1或与数据(5)相同:ymax,ymin,x,y

然后,如果我在ggplot x = log10(Ex $ Length)之前写,它就可以了 . 但是色带在值的位置停止,并且直到极端值才延伸
enter image description here

但是,如果我尝试更改x,如x = seq(0.1,1,by = 1),我会收到错误消息

错误:美学必须是长度1或与数据(5)相同:ymax,ymin,x,y

就像geom_ribbon没有看到我使用一个函数来绘制功能区的上限和下限 . 如果我删除geom_errorbarh它会完美运行,但我需要它 .

有人会有一个解决方案,迫使色带延伸到确定的限制?非常感谢你!

1 回答

  • 1

    我想这就是你想要的

    x=log10(Ex$Length)
    x_new = c(0,log10(Ex$Length), 1)
    z_new = data.frame(x = x_new, ymin = 0.2338  -1.8895*x_new-0.1033*x_new^2, ymax = 0.3186 -2.1155*x_new+0.1197*x_new^2)
    ggplot(data=Ex)+ 
       geom_point(aes(x=log10(Length), y=log10(Perf), color=Temperature,shape=Level), size = 2)+
       geom_errorbarh(aes(x=log10(Length),y=log10(Perf), xmax = log10(Ex$Length + Ex$Length.sd), xmin = log10(Ex$Length - Ex$Length.sd), height = 0.01, color = Temperature))+
       geom_errorbar(aes(x=log10(Length),ymax = log10(Ex$Perf + Ex$Perf.sd), ymin = log10(Ex$Perf - Ex$Perf.sd), width = 0.01,  color = Temperature))+
       scale_color_manual(values=c("blue"))+
       scale_shape_manual(values=c(7, 15, 17, 19, 6))+
       stat_function(fun=function(z){0.2746- 1.9945*z}, colour="red", size=1) +
       geom_ribbon(data = z_new, aes(x = x, ymin= ymin, ymax = ymax), fill="red", colour= NA, alpha=.2) + #CI 1
       theme_bw() + 
       scale_x_continuous(limits = c(0,1), "Length") + 
       scale_y_continuous("Perf")
    

    无论出于何种原因, stat_function 正在评估图表x轴限制范围内的函数,而不是数据集中变量Length的限制 . 但是, geom_ribbon 仅在变量Length的范围内绘制,这就是它们不匹配的原因 . 我不确定为什么会这样,但这就是我要绕过它的方式 . 首先使用 scale_x_continuous(limits = c(0,1), "Length") 决定x轴的限制 . 接下来,创建一个跨越x轴极限范围的新x变量( x_new ) . 然后,根据您的等式( z_new )创建一个数据框,用于评估 x_new 的每个值的ymin和ymax . 在 geom_ribbon 函数中使用此数据集 . 但要做到这一点,你需要从ggplot函数的第一行中删除美学,并为图中的每个元素单独指定它们( geom_pointgeomerrorbarhgeom_errorbar ) . 这是令人讨厌的,但是必须在图的其他方面中将来自主数据集外部的变量合并 .

    更简单的方法是使用 stat_smooth 来运行和绘制线性模型 . 此功能自动绘制95%置信区间(可以通过添加 level = some number between 0 and 1 来更改) . 要禁止自动生成CI,请使用 se = FALSE . 它只会将数据绘制到数据中变量Length的范围内 . 然后,您可以使用已经指定的geom_ribbon . 如果没有理由将该行扩展到数据之外,这就容易多了 .

    ggplot(data=Ex, aes(x=log10(Length), y=log10(Perf)))+ 
     geom_point(aes(shape=Level), size = 2, color = "blue")+
     geom_errorbarh(aes(xmax = log10(Ex$Length + Ex$Length.sd), xmin = 
     log10(Ex$Length - Ex$Length.sd), height = 0.01), color = "blue")+
     geom_errorbar(aes(ymax = log10(Ex$Perf + Ex$Perf.sd), ymin = log10(Ex$Perf - Ex$Perf.sd), width = 0.01), color = "blue")+
     scale_shape_manual(values=c(7, 15, 17, 19, 6))+
     stat_smooth(method = "lm", color = "red", alpha = .2, se = FALSE) +
     geom_ribbon(aes(ymax= 0.3186 -2.1155*x+0.1197*x^2 , 
                  ymin= 0.2338  -1.8895*x-0.1033*x^2 ), fill="red", colour= NA, alpha=.2) + #CI 16
     theme_bw() + 
     scale_x_continuous("Length") + 
     scale_y_continuous("Perf")
    

相关问题