Using the code below, I am able to create the following dot plot:
library(ggplot2)
mtcars$cyl = as.factor(mtcars$cyl)
mtcars$vs = as.factor(mtcars$vs)
p <- ggplot(mtcars, aes(x=cyl, y=mpg, fill = vs))
geom_dotplot(binaxis='y', binwidth = 1, stackdir='center', dotsize = .5, alpha = .5, position=position_dodge(0.25))
p stat_summary(fun.data=mean_sdl,
fun.args = list(mult=1),
geom="pointrange", color=c("black", "red", "black", "red", "black"),
size = 1, show.legend = F, position=position_dodge(0.25))
p
However, if I were to add a theme p theme_bw()
, the geom=pointrange
, features disappear (see screenshot below). Any idea on how I can retain the pointrange information while applying a theme?
CodePudding user response:
The issue is because you aren't storing the plot in p
when you add stat_summary
so you are just seeing the plot before you run stat_summary
. This is why the bottom plot is what shows up when you run p
. What you need to do is
p <- ggplot(mtcars, aes(x=cyl, y=mpg, fill = vs))
geom_dotplot(binaxis='y', binwidth = 1, stackdir='center', dotsize = .5, alpha = .5, position=position_dodge(0.25))
theme_bw()
p <- p stat_summary(fun.data=mean_sdl,
fun.args = list(mult=1),
geom="pointrange", color=c("black", "red", "black", "red", "black"),
size = 1, show.legend = F, position=position_dodge(0.25))
p