使用geom_bar更正geom_errorbar位置

时间:2015-05-17 13:01:46

标签: r ggplot2 geom-bar

这里的数据: https://www.dropbox.com/s/ezvky6geggpyryu/heights.csv?dl=0

我正在尝试调整误差线的位置以匹配柱的中点。

有关如何执行此操作的任何建议?

我已经检查了类似的帖子,但它并没有完全回答我的问题,因为我已经使用该功能设置了我的y位置。 How to make dodge in geom_bar agree with dodge in geom_errorbar, geom_point

这是我的代码:

heights<-read.csv("heights.csv",as.is=T)
heights$line <- factor(heights$line, levels=c("Klein157 (1-3)","Klein157 (4-4)", "Maringa (4-2)", "Maringa (5-6)"))

library("ggplot2")
library("scales")

##DATA SUMMARY##
##code for data summary from 
## http://www.cookbook-r.com/Manipulating_data/Summarizing_data/
##    data: a data frame.
##   measurevar: the name of a column that contains the variable to be summariezed
##   groupvars: a vector containing names of columns that contain grouping variables
##   na.rm: a boolean that indicates whether to ignore NA's
##   conf.interval: the percent range of the confidence interval (default is 95%)


summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
  require(plyr)

  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }

  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )

  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))

  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean

  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult

  return(datac)
}

##using function to summarise data - RIL4 and Cappelle

summarySE(heights, measurevar="height", groupvars=c("line", "allele"))
summary.h<-summarySE(heights, measurevar="height", groupvars=c("line", "allele"))

ggplot(summary.h, aes(x=line, y= height, fill=allele)) +
  geom_bar(stat = "identity", position="dodge", show_guide=TRUE) + geom_bar(stat = "identity", position="dodge", color="black", show_guide=FALSE) +
  geom_errorbar(aes(ymin=height-se, ymax=height+se), position="dodge", size=1, width=0.5) +
  ylab("Height (cm)") +
  xlab("") +
  scale_y_continuous(breaks=pretty_breaks(n=6)) +
  coord_cartesian(ylim=c(65,100)) +
  scale_x_discrete(breaks = c("Klein157 (1-3)","Klein157 (4-4)", "Maringa (4-2)", "Maringa (5-6)")) +
  scale_fill_manual(values = c("#56B4E9", "#009E73")) +
  theme(panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank(),
        plot.title = element_text(lineheight=.4, face="bold"),
        axis.title = element_text(size=25, face="bold", colour="black"), 
        axis.text.y = element_text(size=20, colour="black"),
        axis.text.x = element_text(vjust=0.5, size=20,face="bold", colour="black"),
        strip.text.x =element_text(size=22, face="bold"),
        strip.background=element_rect(colour = "black"),
        legend.text= element_text(size = 22),
        legend.title=element_text(size=18),
        legend.position="top") +
  guides(fill=guide_legend(title=NULL))

1 个答案:

答案 0 :(得分:1)

我找到了解决这个问题的方法。 经过反复试验,我确定了     躲闪&LT; -position_dodge(宽度= 1)

然后使用

geom_errorbar(aes(ymin=height-se, ymax=height+se, x=line), position=dodge, size=1, width=0.5)