【问题标题】:ggplot : Adding mean/error bars for dotplot with different groupsggplot :为具有不同组的点图添加均值/误差条
【发布时间】:2018-01-31 14:51:51
【问题描述】:

我是 ggplot 新手,从这张图开始:

library(ggplot2)
library(reshape2)

data <- read.delim(textConnection("
Sample Day_0 Day_1 Day_4 Day_5 Day_7
NM 1000 221000 6620000 17200000 43700000
OG 1000 351000 1750000 6880000 18300000
OD 1000 961000 1090000 6380000 4400000
ODD 1000 1060000 3550000 12000000 13100000"), sep = " ")

data_melt <- melt(data, id.var = "Sample")
data_melt$value <- as.numeric(data_melt$value)

ggplot(data=data_melt, aes(x=variable, y=value, color = Sample)) + geom_point(size = 2.5) + scale_y_continuous(trans=log2_trans(), breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) + 
  ggtitle("My_Title") + xlab("My_X") + ylab("My_Axis") + theme(plot.title = element_text(hjust = 0.5)) + expand_limits(y = c(10^3, 10^8))

see the graph result

我想做的是添加每个“天”的 4 个点的平均值和误差线(in this kind of way for example,图片来自http://www.sthda.com/)。

任何方法/建议都会有所帮助!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用geom_errorbar 执行此操作,并在定义数据集时添加相关统计信息。对于误差线的长度,下面的代码仅使用 0.25/0.75 经验分位数。如果您想更改它,只需将lowerupper 更改为您感兴趣的范围即可。

    library(dplyr)    
    data_melt <- data_melt %>% group_by(variable) %>% mutate(upper =  quantile(value, 0.75), 
                                                         lower = quantile(value, 0.25),
                                                         mean = mean(value))
    
    # How the first 9 values of your data set should look like now: 
    #A tibble: 20 x 6
    ## Groups: variable [5]
    #Sample variable    value    upper    lower     mean
    #<fctr> <fctr>      <dbl>    <dbl>    <dbl>    <dbl>
    #1 NM     Day_0        1000     1000     1000     1000
    #2 OG     Day_0        1000     1000     1000     1000
    #3 OD     Day_0        1000     1000     1000     1000
    #4 ODD    Day_0        1000     1000     1000     1000
    #5 NM     Day_1      221000   985750   318500   648250
    #6 OG     Day_1      351000   985750   318500   648250
    #7 OD     Day_1      961000   985750   318500   648250
    #8 ODD    Day_1     1060000   985750   318500   648250
    #9 NM     Day_4     6620000  4317500  1585000  3252500
    
    ggplot(data=data_melt, aes(x=variable, y=value, color = Sample)) + 
          geom_point(size = 2.5) + scale_y_continuous(trans=log2_trans(), 
                                              breaks = trans_breaks("log10", 
                                                       function(x) 10^x), 
                                              labels = trans_format("log10", 
                                                        math_format(10^.x))) + 
          ggtitle("My_Title") + 
          xlab("My_X") + ylab("My_Axis") + 
          theme(plot.title = element_text(hjust = 0.5)) + 
          expand_limits(y = c(10^3, 10^8)) + 
          geom_errorbar(aes(ymin = lower, ymax = upper),col = "red", 
                       width =  0.25) +
          geom_point(aes(x = variable, y = mean), size = 3, col = "red")
    

    【讨论】:

    • 这就是我需要的!但我没能重现你所拥有的......你能详细说明你的第一行代码吗?
    • 第一行代码通过添加带有mutate 的相应列来定义data_melt 的分位数和方法。您的案例中产生的错误是什么? dplyr 加载包了吗?
    • 啊,dplyr确实有问题。我再次安装它,然后一切正常。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2019-01-18
    • 2019-09-17
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多