对数刻度图中的误差线?

Errorbars in a log scale plot?

那么,在对数刻度上绘制点时显示误差线的正确方法是什么?因为误差条在绝对尺度上是对称的,我认为它们在对数尺度上是不对称的。但是,使用以下代码,它们在对数刻度上显示为对称。我最初的问题是 'Is the code displaying the error bars properly?' 看了一会儿之后,我有点不确定。

library(ggplot2
pde=1.1 #position dodge for error bars
pdp=0.35 #position dodge for points
p<-ggplot(data=mtcars, aes(x=vs, y=mpg, colour=factor(am)))+
  geom_point(position=position_dodge(width=pdp), size=3)+
  stat_summary( fun = "mean", geom="point", size=2,stroke=1.1, position=position_dodge(width=pde))+ 
  stat_summary( fun.data = "mean_se", geom = "errorbar", width=0.15, position=position_dodge(width=pde))+
  scale_y_log10(limits = c(1,150))

coord_trans()的帮助解释了比例变换(例如scale_y_log10())在计算统计之前执行,而坐标变换(例如coord_trans(y="log10"))在统计之后执行计算。

在您的情况下,这意味着 scale_y_log10 均值和 se 是根据对数转换数据而不是原始未转换数据计算的。要计算未转换数据的统计信息,请删除 scale_y_log10() 并使用 coord_trans(y="log10").

下面的示例显示了 ggplot 内部计算的值,然后通过直接计算重现这些值:

library(tidyverse)

pde=1.1 #position dodge for error bars
pdp=0.35 #position dodge for points

p1 = ggplot(data=mtcars, aes(x=vs, y=mpg, colour=factor(am))) +
  geom_point(position=position_dodge(width=pdp), size=3) +
  stat_summary(fun = "mean", geom="point", size=2, stroke=1.1,
               position=position_dodge(width=pde)) +
  stat_summary( fun.data = "mean_se", geom = "errorbar", 
                width=0.15, position=position_dodge(width=pde)) +
  theme_bw() 

p2 = p1 + scale_y_log10() 

# Get data frames for each set of mean/errorbar layers
#  that ggplot calculates internally 
p1dat = ggplot_build(p1)$data[[3]]
p2dat = ggplot_build(p2)$data[[3]]

p1dat %>% select(y, ymin, ymax)
#>          y     ymin     ymax
#> 1 15.05000 14.24910 15.85090
#> 2 20.74286 19.80888 21.67683
#> 3 19.75000 18.11339 21.38661
#> 4 28.37143 26.57319 30.16967

p2dat %>% select(y, ymin, ymax) %>% 
  mutate(y.trans = 10^y,
         ymax.trans = 10^ymax)
#>          y     ymin     ymax  y.trans ymax.trans
#> 1 1.170219 1.145648 1.194790 14.79853   15.65992
#> 2 1.314225 1.294657 1.333793 20.61699   21.56718
#> 3 1.288104 1.252044 1.324165 19.41353   21.09431
#> 4 1.447286 1.418346 1.476226 28.00826   29.93823

现在通过直接计算重现这些相同的值:

mtcars %>% 
  group_by(am, vs) %>% 
  summarise(mean = mean(mpg),
            mean.log = mean(log10(mpg)),
            mean.log.trans = 10^mean.log,
            mean.plus.se = mean + sqrt(var(mpg)/length(mpg)),
            se.log = sqrt(var(log10(mpg))/length(mpg)),
            mean.log.plus.se = mean.log + se.log,
            mean.log.plus.se.trans = 10^mean.log.plus.se)

#>   am vs     mean mean.log mean.log.trans mean.plus.se     se.log
#> 1  0  0 15.05000 1.170219       14.79853     15.85090 0.02457101
#> 2  0  1 20.74286 1.314225       20.61699     21.67683 0.01956814
#> 3  1  0 19.75000 1.288104       19.41353     21.38661 0.03606088
#> 4  1  1 28.37143 1.447286       28.00826     30.16967 0.02893993
#>   mean.log.plus.se mean.log.plus.se.trans
#> 1         1.194790               15.65992
#> 2         1.333793               21.56718
#> 3         1.324165               21.09431
#> 4         1.476226               29.93823

而且我们还可以看到coord_trans(y="log10")在对数变换之前计算均值和误差线:

p3 = p1 + coord_trans(y="log10")
p3dat = ggplot_build(p3)$data[[3]]

p3dat %>% select(y, ymin, ymax)
#>          y     ymin     ymax
#> 1 15.05000 14.24910 15.85090
#> 2 20.74286 19.80888 21.67683
#> 3 19.75000 18.11339 21.38661
#> 4 28.37143 26.57319 30.16967