Combine stack and dodge with bar plot in ggplot2

后端 未结 1 2065
误落风尘
误落风尘 2021-01-18 10:17

I\'m trying to recreate this plot without the horrible 3d bar plot and the unclear x axis (these are distinct timepoints and it\'s hard to tell when they are).

1条回答
  •  鱼传尺愫
    2021-01-18 10:24

    It seems to me that a line plot is more intuitive here:

     library(forcats)
    
     data %>% 
       filter(!is.na(`Mean % of auxotrophs`)) %>%
       ggplot(aes(x = Timepoint, y = `Mean % of auxotrophs`, 
                  color = fct_relevel(Mutator, c("o","m","n")), linetype=`Ancestral genotype`)) +
       geom_line() +
       geom_point(size=4) + 
       labs(linetype="Ancestral\ngenotype", colour="Mutator")
    

    To respond to your comment: Here's a hacky way to stack separately by Ancestral genotype and then dodge each pair. We plot stacked bars separately for mutS- and mutS+, and dodge the bars manually by shifting Timepoint a small amount in opposite directions. Setting the bar width equal twice the shift amount will result in pairs of bars that touch each other. I've added a small amount of extra shift (5.5 instead of 5) to create a tiny amount of space between the two bars in each pair.

     ggplot() +
       geom_col(data=data %>% filter(`Ancestral genotype`=="mutS+"),
                aes(x = Timepoint + 5.5, y = `Mean % of auxotrophs`, fill=Mutator),
                width=10, colour="grey40", size=0.4) + 
       geom_col(data=data %>% filter(`Ancestral genotype`=="mutS-"),
                aes(x = Timepoint - 5.5, y = `Mean % of auxotrophs`, fill=Mutator), 
                width=10, colour="grey40", size=0.4) + 
       scale_fill_discrete(drop=FALSE) +
       scale_y_continuous(limits=c(0,26), expand=c(0,0)) +
       labs(x="Timepoint")
    

    Note: In both of the examples above, I've kept Timepoint as a numeric variable (i.e., I skipped the step where you converted it to character) in order to ensure that the x-axis is denominated in time units, rather than converting it to a categorical axis. The 3D plot is an abomination, not only because of distortion due to the 3D perspective, but also because it creates a false appearance that each measurement is separated by the same time interval.

    0 讨论(0)
提交回复
热议问题