r - Combine stack and dodge with bar plot in ggplot2 -


i'm trying recreate plot without horrible 3d bar plot , unclear x axis (these distinct timepoints , it's hard tell when are).

bad plot

(from science 291, no. 5513 (2001): 2606–8, otherwise paper.)

my first instinct similar did, 2d bar plot , distinct x axis labels, using dodged bars genotype , stacked bars black , white split on front bar, several other questions here can't that.

my next approach use faceting (code below), worked reasonably well, i'd love see better way this. there way stack variables , doge others? or better way in general?

edit: clarify, think important show total of stacked bars (m , n in case, black , white originally), because represents measured quantity, , split separate measurement.

this faceted version.

library(tidyverse) library(cowplot)  data = tribble(   ~timepoint, ~`ancestral genotype`, ~mutator, ~`mean % of auxotrophs`,   100, 'muts-', 'o', 10.5,   150, 'muts-', 'o', 16,   220, 'muts-', 'o', na,   300, 'muts-', 'o', 24.5,   100, 'muts+', 'n', 1,   150, 'muts+', 'n', na,   220, 'muts+', 'n', 1,   300, 'muts+', 'n', 1,   100, 'muts+', 'm', 0,   150, 'muts+', 'm', na,   220, 'muts+', 'm', 2,   300, 'muts+', 'm', 5 )  data <- data %>% mutate(timepoint = as.character(timepoint))  data %>% ggplot(aes(x = timepoint, y = `mean % of auxotrophs`)) +   geom_col(aes(fill = mutator), position = 'stack') + facet_grid(~`ancestral genotype` ) +   guides(fill=false) 

it seems me line plot 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") 

enter image description here

to respond comment: here's hacky way stack separately ancestral genotype , dodge each pair. plot stacked bars separately muts- , muts+, , dodge bars manually shifting timepoint small amount in opposite directions. setting bar width equal twice shift amount result in pairs of bars touch each other. i've added small amount of shift (5.5 instead of 5) create tiny amount of space between 2 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") 

enter image description here

note: in both of examples above, i've kept timepoint numeric variable (i.e., skipped step converted character) in order ensure x-axis denominated in time units, rather converting categorical axis. 3d plot abomination, not because of distortion due 3d perspective, because creates false appearance each measurement separated same time interval.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -