ggplot2: horizontal position of stat_summary with geom_boxplot - r

I want to annotate mean of each boxplot using ggplot2. However, I could not figure out how to horizontally center the symbols marking the means within their respective boxes (see image below).
MWE is below for reference:
library(ggplot2)
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
geom_boxplot(aes(color = drv), outlier.shape = NA) +
stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv)) +
theme_bw()

Try with position_dodge()
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
geom_boxplot(aes(color = drv), outlier.shape = NA) +
stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv),
position = position_dodge(width = .75)) +
theme_bw()

Related

How to add y value average text to geom_bar?

ggplot(aes(x=MALE, y=AMOUNT, fill=MALE)) + geom_bar(stat="summary", fun="mean") +
ylab("Avg Amount") + theme(axis.title.x = element_blank())
How can I add the y value to the top of the bars given I've already created stat='summary' & fun='mean' when I created the graph?
To add the y value as label on top of your bars you can do:
geom_text(aes(label = after_stat(y)), stat = "summary", fun = "mean", vjust = -.1)
Using mtcars as example data and with some additional formatting of the label:
library(ggplot2)
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
geom_bar(stat = "summary", fun = "mean") +
geom_text(aes(label = after_stat(sprintf("%.1f", y))), stat = "summary", fun = "mean", vjust = -.1) +
ylab("Avg Amount") +
theme(axis.title.x = element_blank())

coord_trans and coord_flip in same plot

I am trying to use both coord_trans and coord_flip in the same plot, but that seems to not work. Any suggestion how to use coord_trans for a plot that needs to be flipped?
Using scale_y_log10 does not work since it messes up the stat_summary
p <- ggplot(df.2,aes(reorder(x,y),y,colour=z)) +
geom_jitter(width = 0.2,size=0.1) +
theme_classic(base_size = 8) +
stat_summary(
fun = mean,
geom = "errorbar",
aes(ymax = ..y.., ymin = ..y..),
position = position_dodge(width = 0.1),
width = 0.7,
colour="black") +
coord_flip() +
theme(legend.position = "none") +
labs(x="",y="") +
scale_color_manual(values = mycolors)
p + coord_trans(y = "log10")

ggplot multiple boxplots and stat_summary position

I have the following code. I'd like to change the color of the boxplots so they all have the same fill color (grey).
Also I'd like to have the stat_summary texts to stick to the bottom of each barplot but vjust only seem to provide relative position?
Thanks
boxp <- ggplot(mtcars, aes(as.factor(cyl), wt, fill=as.factor(am)) ) +
geom_bar(position = "dodge", stat = "summary", fun.y = "median") +
geom_boxplot(outlier.shape = NA, width=0.2, color = "black", position = position_dodge(0.9)) +
stat_summary(aes(label=round(..y..,2)), fun.y=median, geom="text", size=8, col = "white", vjust=8, position = position_dodge(0.9)) +
stat_summary(fun.y=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
boxp
Here is a possible solution, but it needs ggplot v3.3.0 for the stage() function.
To point out major changes:
Instead of using the fill as an implicit grouping, I've explicitly set the grouping so it isn't tied to the fill.
I added the fill as an aesthetic of the bar geom.
The boxplot now has the unmapped aesthetic fill = 'gray'
The text stat summary uses stage() to calculate the statistic but then uses 0 as actual placement.
library(ggplot2)
library(scales)
ggplot(mtcars, aes(as.factor(cyl), wt,
group = interaction(as.factor(cyl), as.factor(am)))) +
geom_bar(aes(fill=as.factor(am)), position = "dodge", stat = "summary", fun = "median") +
geom_boxplot(outlier.shape = NA, width=0.2,
color = "black", fill = 'gray',
position = position_dodge(0.9)) +
stat_summary(aes(label=round(after_stat(y), 2), y = stage(wt, after_stat = 0)),
fun=median, geom="text", size=8, col = "white", vjust=-0.5,
position = position_dodge(0.9)) +
stat_summary(fun=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
Created on 2020-05-06 by the reprex package (v0.3.0)

ggplot2: is there a way to align the mean to the box plot and to align the legend text? [duplicate]

I want to annotate mean of each boxplot using ggplot2. However, I could not figure out how to horizontally center the symbols marking the means within their respective boxes (see image below).
MWE is below for reference:
library(ggplot2)
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
geom_boxplot(aes(color = drv), outlier.shape = NA) +
stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv)) +
theme_bw()
Try with position_dodge()
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
geom_boxplot(aes(color = drv), outlier.shape = NA) +
stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv),
position = position_dodge(width = .75)) +
theme_bw()

R ggplot2 stat_summary legend mean additional to groups

I've plotted the mean for the whole study population (black line) and for men and women separately.
plotYYIR1<- ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
labs(x="Week number", y="YYIR1 distance run (m)") +
theme(plot.title = element_text(hjust = 0, vjust=0))+
theme(legend.title=element_blank())+
theme(legend.key.width = unit(1, "cm"))+
stat_summary(fun.y = mean,geom = "point", size=2) +
stat_summary(fun.y = mean, geom = "line", size=0.7) +
stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) +
scale_shape_manual(values = c("Male"=17, "Female"=15))+
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) +
scale_colour_manual(values = c("#009CEF", "#CC0000"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2)+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex))
plotYYIR1
The legend only shows the genders, could someone help me with adding the black line and points in the legend for the whole group?
You need to add aes() to get a legend for the black line/points. If you want the legend to be for lines/shapes combined you can turn off the legend for shapes by adding guide = F to scale_shape_manual and then use override.aes in guides to specify the shapes in the legend:
ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
labs(x="Week number", y="YYIR1 distance run (m)") +
theme(plot.title = element_text(hjust = 0, vjust=0))+
theme(legend.title=element_blank())+
theme(legend.key.width = unit(1, "cm"))+
stat_summary(fun.y = mean,geom = "point", size=2, aes(colour = "mean")) +
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour = "mean")) +
stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) +
scale_shape_manual(values = c("Male"=17, "Female"=15, "mean"=16), guide = F)+
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) +
scale_colour_manual(values = c("#009CEF", "#CC0000", "#000000"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour = "mean"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex)) +
guides(colour = guide_legend(override.aes = list(shape = c("Male"=17, "Female"=15, "mean"=16))))

Resources