2015-05-17 6 views
0

данных здесь: https://www.dropbox.com/s/ezvky6geggpyryu/heights.csv?dl=0исправляя положение geom_errorbar с geom_bar

Я пытаюсь настроить положение погрешностей, чтобы соответствовать середине точки прутов.

Любые предложения о том, как это сделать?

Я проверил подобный пост, но он не совсем отвечает на мой вопрос, так как я уже устанавливаю свое положение 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)) 

ответ

0

Я нашел способ обойти это. Методом проб и ошибок, я установил увернуться < -position_dodge (ширина = 1)

, а затем использовали

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

 Смежные вопросы

  • Нет связанных вопросов^_^