使用geom =“ errorbars”的错误栏的位置错误

时间:2018-12-01 18:25:01

标签: r

我正在尝试将errorbars添加到我创建的图形中: enter image description here

如您所见,错误栏不在正确的位置,我不知道为什么不正确。以下是我使用的代码:

df_midtermclean$gender <- factor(df_midtermclean$ï..gender, labels = c('Male', 'Female'))  

#reshaping from wide to long format

data_long <- melt(df_midtermclean,
                  # ID variables - all the variables to keep but not split apart on
                  id.vars=c("gender"),
                  # The source columns
                  measure.vars=c("sSkills", "sPerform", "sComplex", "sIQ" ),
                  # Name of the destination column that will identify the original
                  # column that the measurement came from
                  variable.name="measures",
                  value.name="score")        

p2 <- ggplot(data_long, aes(measures, score)) + stat_summary(fun.y = mean, geom = "bar", aes(fill = gender), position = "dodge") 
    p2 <- p2 + stat_summary(geom = "errorbar", fun.data = mean_cl_normal, position = position_dodge(.9), width = .1)
    p2 <- p2 + labs(x = "Continuous Measures", y = "Participant Scores", fill = "Gender")
    p2

数据。

data_long <-
structure(list(gender = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 2L,  1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
2L, 2L, 1L, 1L, 1L), .Label = c("Male",  "Female"), 
class = "factor"), measures = structure(c(1L, 1L,  
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L,  1L, 1L), .Label = c("sSkills", 
"sPerform", "sComplex", "sIQ"), class = "factor"), 
score = c(4.75, 3, 3.5, 3.75, 3, 3.25, 3.25, 3, 
2.75, 2.75, 2.5, 2.75, 3.75, 3.5, 3.75, 3.75, 
3.25, 3.25, 4, 3.25)), row.names = c(NA,  20L), 
class = "data.frame")

3 个答案:

答案 0 :(得分:1)

不幸的是,您的代码不可复制!但是,我可能在http://www.sthda.com/english/wiki/ggplot2-error-bars-quick-start-guide-r-software-and-data-visualization

上找到了解决方案
library(ggplot2)

df <- ToothGrowth
df$dose <- as.factor(df$dose)

# Function to calculate the mean and the standard deviation
data_summary <- function(data, varname, groupnames){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sd(x[[col]], na.rm=TRUE))
  }


 data_sum <- ddply(data, groupnames, .fun=summary_func, varname)
 data_sum <- rename(data_sum, c("mean" = varname))
 return(data_sum)
}

# New dataframe
df2 <- data_summary(ToothGrowth, varname="len", 
                    groupnames=c("supp", "dose"))

df2$dose <- as.factor(df2$dose)

ggplot(df2, aes(x=dose, y=len, fill=supp)) + 
  geom_bar(stat="identity", color="black", position=position_dodge()) +
  geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2, position=position_dodge(.9)) 

enter image description here

答案 1 :(得分:0)

我认为您的代码的第二行应采用以下格式:

 geom_errorbar(aes(ymin = lower, ymax = upper), position = dodge, width = 0.25)

我不确定您的代码中包含什么stat_summary,您能解释一下吗?但是,您需要明确提及误差线的上限和下限,我认为您的代码中没有包含该上限。

编辑:尝试调整position = position = position_dodge(0.9)以及大小和宽度。

答案 2 :(得分:0)

我相信以下内容将满足您的需求。

agg <- aggregate(score ~ gender + measures, data_long, function(x) c(mu = mean(x), se = sd(x)))
agg <- cbind(agg[-ncol(agg)], agg[[ncol(agg)]])

p2 <- ggplot(agg, aes(measures, mu, fill = gender, group = gender)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mu - se, ymax = mu + se),
                position = position_dodge(0.9),
                width = 0.1)

p2

enter image description here