ggplot2:使对数stat_smooth适合geom_ribbon

时间:2018-08-10 06:58:01

标签: r ggplot2

我正在尝试在ggplot2中绘制拟合模型效果,以替代effects包返回的图,并且在使用stat_smooth拟合时遇到了问题通过geom_ribbon进行对数转换的置信带。与geom_ribbon的典型用法不同,我不需要计算频段-eff对象为我提供了频段的限制-我只需要对它们进行对数转换。关于geom_line(例如,R, ggplot2: Fit curve to scatter plot)的操作方法,目前有很多,但是到目前为止,我还没有找到geom_ribbon的任何东西。

数据:

myEffs <- structure(list(TargetVowelDur = c(0.03, 0.4, 0.8, 1, 2), fit = c(-0.467790933985126, 
0.823476426481035, 1.16901542809292, 1.28025414059112, 1.625793142203
), se = c(0.087385175843338, 0.0895697786138634, 0.0922444075008412, 
0.0932736493340376, 0.0969532573361368), lower = c(-0.639066303684154, 
0.647919224725754, 0.98821594070963, 1.09743733420847, 1.43576428623844
), upper = c(-0.296515564286098, 0.999033628236315, 1.34981491547621, 
1.46307094697376, 1.81582199816757)), class = "data.frame", row.names = c(NA, 
-5L), transformation = function (eta) 
eta, .Names = c("TargetVowelDur", "fit", "se", "lower", "upper"
))

按原样传递geom_line会产生4条相连的线段,而不是对数曲线,因此标准解决方案是添加stat_smooth

library(ggplot2)
p1 <- ggplot(myEffs, aes(x=TargetVowelDur, y=fit)) +
  geom_line(stat="smooth", method="lm", formula=y~log(x))
p1

enter image description here

一切都很好。按照同样的逻辑,我们应该能够在stat_smooth上添加geom_ribbon,但这样做不会使图保持不变

p2 <- p1 + 
  geom_ribbon(aes(ymin=lower, ymax=upper), stat="smooth", method="lm", formula=y~log(x))
p2

enter image description here

如果我们窥探p2的构建,尽管ymin的事实,我们发现ymax的{​​{1}}和geom_ribbon是相同的和upper列不相同:

lower

我如何让> print(lapply(ggplot_build(p2)$data, head)) [[1]] x y ymin ymax se PANEL group colour size linetype alpha 1 0.03000000 -0.46779093 -0.46779093 -0.46779093 2.568169e-15 1 -1 black 0.5 1 NA 2 0.05493671 -0.16620173 -0.16620173 -0.16620173 2.136541e-15 1 -1 black 0.5 1 NA 3 0.07987342 0.02037031 0.02037031 0.02037031 1.887702e-15 1 -1 black 0.5 1 NA 4 0.10481013 0.15581841 0.15581841 0.15581841 1.720023e-15 1 -1 black 0.5 1 NA 5 0.12974684 0.26221720 0.26221720 0.26221720 1.598524e-15 1 -1 black 0.5 1 NA 6 0.15468354 0.34985293 0.34985293 0.34985293 1.506906e-15 1 -1 black 0.5 1 NA [[2]] x y ymin ymax se PANEL group colour fill size linetype alpha 1 0.03000000 -0.46779093 -0.46779093 -0.46779093 2.568169e-15 1 -1 NA grey20 0.5 1 NA 2 0.05493671 -0.16620173 -0.16620173 -0.16620173 2.136541e-15 1 -1 NA grey20 0.5 1 NA 3 0.07987342 0.02037031 0.02037031 0.02037031 1.887702e-15 1 -1 NA grey20 0.5 1 NA 4 0.10481013 0.15581841 0.15581841 0.15581841 1.720023e-15 1 -1 NA grey20 0.5 1 NA 5 0.12974684 0.26221720 0.26221720 0.26221720 1.598524e-15 1 -1 NA grey20 0.5 1 NA 6 0.15468354 0.34985293 0.34985293 0.34985293 1.506906e-15 1 -1 NA grey20 0.5 1 NA > myEffs$upper - myEffs$lower [1] 0.3425507 0.3511144 0.3615990 0.3656336 0.3800577 stat_smooth一起好玩?

1 个答案:

答案 0 :(得分:1)

我的解决方案是绘制三行(数据,上下),然后使用“上”和“下”线的数据创建一个灰色区域;丝带。

library(ggplot2)
g1 <- ggplot(myEffs) + 
  geom_line(aes(x = TargetVowelDur, y = fit), stat = "smooth", method = "lm", formula=y~log(x)) + 
  geom_line(aes(x = TargetVowelDur, y = upper), color = "red", stat = "smooth", method = "lm", formula=y~log(x)) + 
  geom_line(aes(x = TargetVowelDur, y = lower), color = "blue", stat = "smooth", method = "lm", formula=y~log(x))

g1

# build plot object for rendering 
gg1 <- ggplot_build(g1)

# extract data from the upper and lower lines
df2 <- data.frame(x = gg1$data[[1]]$x,
                  ymin = gg1$data[[2]]$y,
                  ymax = gg1$data[[3]]$y) 

# use the lm data to add the ribbon to the plot 
g1 +  geom_ribbon(data = df2, aes(x = x, ymin = ymin, ymax = ymax), fill = "grey", alpha = 0.4)

enter image description here

基于this post中@Henrik的答案