How to add space in boxplot and center the median

5

I would like to make two changes to the following chart

:

  • Leaveasmallspacebetweenthegreen(F)boxplotandorange(s)

  • Placethemedianpointinthecenterofeachboxplot(pointsareappearingattheendoftheboxplotandwhenthetwoplotshavethesamemedianvalue,onlyonepointisplotted).

    /li>

    DoesanyoneknowwhatchangesIshouldmakeinmyscript?

    Myspreadsheetcanbeaccessedthroughthelink: link

    Thanks in advance for any help, I'll leave the sampling below:

    library(ggplot2)
    
    head(dados)
    dados$warfare <- factor(dados$warfare, levels=c("war1", "war2", "Post"))
    
    ggplot(dados, aes(x=warfare, y=Abund, group=warfare:habitat_F_S, colour = as.factor(habitat_F_S), fill=habitat_F_S)) +
      geom_boxplot(outlier.shape = NA) + 
      scale_fill_brewer(palette="Dark2") + 
      scale_colour_brewer(palette="Dark2") + 
      stat_summary(fun.y=median, geom="line", lwd=1, aes(group=habitat_F_S, colour=habitat_F_S)) + 
      stat_summary(fun.y=median, colour="black", geom="point", shape=18, size=1,show_guide = FALSE, aes(group=habitat_F_S, colour=habitat_F_S)) +
      facet_wrap(~ specie, nrow=5) + 
      labs(x="time", y="Abund", colour="Habitat", fill="Habitat") + 
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
    
        
    asked by anonymous 29.05.2018 / 17:39

    2 answers

    3

    To change the plot position of the groups use the function position=position_dodge(1)

    Example:

    p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
      geom_boxplot(position=position_dodge(1))
    p
    

    Source: STHDA - GGPLOT Guide

        
    29.05.2018 / 18:51
    4

    I believe the following code can help you.

    ggplot(dados, aes(x=warfare, y=Abund, group=warfare:habitat_F_S, colour = as.factor(habitat_F_S), fill=habitat_F_S)) +
      geom_boxplot(outlier.shape = NA, position=position_dodge(1)) + 
      scale_fill_brewer(palette="Dark2") + 
      scale_colour_brewer(palette="Dark2") + 
      stat_summary(fun.y=median, geom="line", lwd=1, aes(group=habitat_F_S, colour=habitat_F_S)) +
      stat_summary(fun.y=median, geom="point", size=2, shape=18, colour="black", show.legend=FALSE, aes(group=habitat_F_S), position=position_dodge(1)) +
      facet_wrap(~ specie, nrow=5) + 
      labs(x="time", y="Abund", colour="Habitat", fill="Habitat") + 
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
    

    Mychangesfromtheoriginalcodewere:

    • geom_boxplot(outlier.shape=NA,position=position_dodge(1)):argumentposition=position_dodge(1)shiftsgreenandorangeboxplots

    • stat_summary(fun.y=median,geom="point", size=2, shape=18, colour="black", show.legend=FALSE, aes(group=habitat_F_S), position=position_dodge(1)): o argumento position = position_dodge (1) 'moves the points to the middle of the boxplots. I painted the black dots to highlight them, because it would not make sense for them to have the color of the boxplots themselves.

    In particular, although I keep geom_boxplot(outlier.shape = NA) , I do not agree with it. Taking the outliers out of your graphics ends up distorting them, so the reader can not figure out what your data really is. This data has many outliers and removing this from the graph is lying about what you are analyzing.

        
    30.05.2018 / 12:40