简体   繁体   English

向R中的自定义条形图功能添加错误条

[英]Add error bars to customized barplot function in R

I have a function to make custom barplots for plotting correlation coefficients that looks like this 我有一个功能可以使自定义条形图绘制出如下所示的相关系数

    olsenbar <- function(i , axisnames = FALSE , data = rs) {
   barplot(
     data[,i] ,
     main = colnames(data)[i] ,
     horiz = TRUE ,
     border = FALSE ,
     space = NULL , col = c ("dimgray") , 
     axes = FALSE ,
     axisnames = axisnames ,
     cex.names = 2 ,
     cex.main = 2 ,
     ylab = "" ,
     xlab = "" ,
     xlim = c(-1,1))
   axis( side = 1 , at = c(-1.0,0,1.0) , cex.axis = 1.6)
   abline( v = 0 , lty = 1 , lwd = 3 , col = grey(0.5))
   abline( v = seq( -1 , 1 , 0.1 ) , lty = 3 )}

which I want to apply to some correlatoins looking like the following. 我想将其应用于如下所示的一些相关因子。 The data includes the correlation coefficients and corresponding confidence intervals and p-values with another variable. 该数据包括相关系数和相应的置信区间以及带有另一个变量的p值。

df
                       lower     r upper    p
Hcy response           -0.28  0.53  0.90 0.18
Cysteine response      -0.48  0.34  0.84 0.41
Methionine response    -0.44  0.38  0.86 0.36
Cystathionine response -0.10  0.65  0.93 0.08
Glutation response     -0.83 -0.31  0.51 0.46
Taurine response       -0.31  0.51  0.89 0.20

When I apply the above function to it like so 当我像这样将上述功能应用于它时

olsenbar( 2 , data = df , axisnames = TRUE )

I end up with the following plot 我最终得到以下情节

Barplot 条形图

However, I would like to enter the corresponding confidence intervals to the bars, but I can't seem to work that out. 但是,我想在条形图中输入相应的置信区间,但似乎无法解决。 As I understand, working out error bars for these barplots is a bit tricky, and I can't get it to work no matter what I try. 据我了解,为这些条形图计算错误栏有些棘手,无论我如何尝试,我都无法使其正常工作。 Any help would be appreciated. 任何帮助,将不胜感激。

With ggplot2 : 使用ggplot2

library(ggplot2)
ggplot(df, aes(names, r)) +
    geom_col(fill = 'dimgray') +
    geom_errorbar(aes(ymin = lower, ymax = upper), width = .5, size = 1) +
    geom_hline(yintercept = 0) +
    scale_y_continuous(limits = c(-1, 1), position = 'top', breaks = c(-1, 0, 1), minor_breaks = seq(-1, 1, .1)) +
    coord_flip() +
    theme(panel.grid = element_blank(),
          panel.grid.major.x = element_line(linetype = 3, colour = 'black'),
          panel.grid.minor.x = element_line(linetype = 3, colour = 'black'),
          panel.background = element_rect('white')
          )

Data: 数据:

df <- read.table(text = 'names lower     r upper    p
"Hcy response"           -0.28  0.53  0.90 0.18
                 "Cysteine response"      -0.48  0.34  0.84 0.41
                 "Methionine response"    -0.44  0.38  0.86 0.36
                 "Cystathionine response" -0.10  0.65  0.93 0.08
                 "Glutation response"     -0.83 -0.31  0.51 0.46
                 "Taurine response"       -0.31  0.51  0.89 0.20', h = T)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM