【问题标题】:boxplot in ggplot with geom_line from different dataggplot中的箱线图与来自不同数据的geom_line
【发布时间】:2019-04-21 16:50:20
【问题描述】:

我有一个名为“绑定”的 df,其中包含不同时间间隔(9_10,10_11,11_12 = 列名)的值(我找到了多少动物)。最后两行是区间内气温的平均值和标准差,来自另一个df。

9_10 10_11 11_12
2.1 5.1 不适用
4.23 2.1 9.2
北美 3.2 5.6
18.56 20.45 23.56
5.67 5.12 5.78

不过我的df要长很多..

现在,我想制作一个箱线图,其中的列名定义 x 轴,而箱线由找到的动物的值组成。平均值应打印为一条线(可能带有第二个 y 轴),其中 sd 作为误差线。不知何故,尽管这些线会位于箱线图之外,因为它们不是来自相同的数据:

https://peltiertech.com/images/2011-06/BoxPlotH5a.png (对不起,这里不允许发图片)

Alpha、Beta 等将是 9_10、10_11 等。

我已经尝试过这个(除其他外):

t <- ggplot(stack(bound[1:3,]), aes(x=ind, y=values))
t <- t + geom_boxplot(outlier.shape=NA,fill="grey", color="black")
t <- t + coord_cartesian(ylim = c(0, 20))
t <- t + scale_x_discrete(name = NULL, labels=c("09:00 - 09:59","10:00 - 10:59","11:00 - 11:59"))
t <- t + scale_y_continuous(name = "animals found per hour")
t <- t + geom_line(stack(bound[4,]),aes(x=ind, y=values)) 
t <- t + scale_y_continuous(sec.axis = sec_axis(~.), name = "mean air temperature")

这段代码给了我一个很好的箱线图,就像我想要它用于找到动物数量的行一样。但是空气温度线没有出现,我不知道 ggplot 是否能够做到。在我看来,它在箱线图中的某处垂直绘制了一条线,但在箱线图之间没有水平线。

谁能帮帮我?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    有两个问题:

    1. 您尝试使用非数字 x 值创建 geom_line
    2. 在添加新的ggplot时需要指定data=,并且数据集与原始ggplot中的数据集不一样

    希望对你有帮助

    编辑:将来,请尝试使用函数dput(bound) 将您的数据集捕获到代码中,以便发布到 SO :)

    # data
    library(ggplot2)
    input <- c(2.1,     5.1  ,    NA,
               4.23,    2.1   ,   9.2,
               NA  ,    3.2   ,   5.6,
               18.56,   20.45 ,   23.56,
               5.67 ,   5.12  ,   5.78)
    bound <- data.frame(matrix(input, ncol=3, byrow = TRUE))
    names(bound) <- c("9_10", "10_11", "11_12")
    
    t <- ggplot(stack(bound[1:3,]), aes(x=ind, y=values))
    t <- t + geom_boxplot(outlier.shape=NA,fill="grey", color="black")
    t <- t + coord_cartesian(ylim = c(0, 20))
    t <- t + scale_x_discrete(name = NULL, labels=c("09:00 - 09:59","10:00 - 10:59","11:00 - 11:59"))
    t <- t + scale_y_continuous(name = "animals found per hour")
    
    # extract the bound[4,]
    error_bars <- stack(bound[4,])
    # replace with your formulation e.g. looks like negative binomial maybe?
    error_bars$low <- error_bars$values-1.96*unlist(bound[5,])
    error_bars$upp <- error_bars$values+1.96*unlist(bound[5,])
    
    # two issues
    # 1. the column ind will have values "9_10" which aren't numeric scale
    #    boxplots have factor levels on x axis. The levels by default will be numeric
    #    vector starting at 1,2,3 etc.
    #    Try replacing ind with these factor levels
    error_bars$ind <- 1:3
    
    
    # 2. best practice to add data=line_df, as without it, ggplot throws a tantrum
    # if you've specified a different data set in the original ggplot
    t <- t + geom_line(data=error_bars, aes(x=ind, y=values)) +
      geom_errorbar(data=error_bars, aes(ymin=low, ymax=upp), colour="indianred2")
    t <- t + scale_y_continuous(sec.axis = sec_axis(~), name = "mean air temperature")
    t <- t + theme_minimal()
    # can now see the line
    t
    

    【讨论】:

    • 哇哦,效果不错!!非常感谢!同样对于 dput 建议.. 现在唯一困扰我的是该线漂浮在箱线图上方。如果我使用line_df &lt;- stack(bound[68,]/2),然后使用t &lt;- t + scale_y_continuous(sec.axis = sec_axis(~./2), name = "mean air temperature"),则该线出现在 y 轴值为 5 处,这是不正确的。你知道如何解决这个问题吗?我还想在行中添加错误栏,它来自bound[5,] 行。对不起,我忘了在原来的问题中问这个......
    • 我进行了编辑以展示如何包含误差线。从外观上看,在 ggplot 中绘制两个轴图并不简单:stackoverflow.com/questions/3099219/…
    • 您可以尝试在单独的窗口上绘制它们,例如cran.r-project.org/web/packages/cowplot/vignettes/…。如果您的比例尺超出,这很困难,但您可以查看数据的转换是否可以对齐比例尺,例如dataminingblog.com/standardization-vs-normalization
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 2019-02-23
    • 2021-01-04
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多