使用geom_bar修正geom_errorbar位置

问题描述 投票:0回答:2

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

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

对于如何做到这一点有什么建议吗?

我已经检查过类似的帖子,但它并不能完全回答我的问题,因为我已经使用该函数设置了 y 位置。 如何让geom_bar中的闪避与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))
r ggplot2 geom-bar
2个回答
1
投票

我找到了解决这个问题的方法。 通过反复试验,我设置 闪避<-position_dodge(width=1)

然后使用

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

0
投票
geom_errorbar(aes(ymin = mean - se, ymax = mean + se), position = position_dodge(0.3))

首先,您必须指定与您的地理栏相对应的位置类型。然后误差条的位置位于 geombar 的中心(此处为 0.3)。

© www.soinside.com 2019 - 2024. All rights reserved.