Ggplot2: Horizontal Position of Stat_Summary with Geom_Boxplot

ggplot2: horizontal position of stat_summary with geom_boxplot

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()

ggplot2 - Error while generating a horizontal boxplot with custom whiskers using geom_boxplot

You need to set orientation = 'y' to get the horizontal boxplot directly from geom_boxplot. You might think geom_boxplot would guess this from the supplied aesthetics, but it doesn't.

ggplot() +
geom_boxplot(data = df_boxplot,
aes(y = x_coord, xmin = y0, xlower = y25, xmiddle = y50,
xupper = y75, xmax = y100),
stat = "identity", orientation = 'y'
) +
geom_dotplot(data = df, aes(x=y))

Sample Image

R ggplot2: Add means as horizontal line in a boxplot

You can add horizontal lines to plots by using stat_summary with geom_errorbar. The line is horizontal because the y minimum and maximum are set to be the same as y.

ggplot(dat, aes(x = study, y = FPKM)) + 
geom_boxplot() +
stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
width = .75, linetype = "dashed")

Sample Image

ggplot2 Issue with stat_summary fun mean not plotting correctly within boxplot

Add position = position_dodge(width = 0.75) to the stat_summary call:


ggplot(airquality_trimmed, aes(x= Month, y= Ozone, fill= Temp.f))+
geom_boxplot(alpha= 0.7)+
scale_y_continuous(name= "Mean ozone in\nparts per billion") +
scale_x_discrete(name= "Month")+
stat_summary(fun= "mean", geom= "point", colour= "black", position = position_dodge(width = 0.75))+
theme_bw()+
scale_fill_brewer(palette = "Accent") +
labs(fill = "Temperature")

Sample Image

Created on 2020-07-08 by the reprex package (v0.3.0)

Add number of observations per group in ggplot2 boxplot

You can just use position:

p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +  
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p

Sample Image

The width argument of position_dodge() controls the positioning on the horizontal axis. 0.75 is the sweet spot, see how it works for different numbers of groupings:

p2 <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(cyl))) + 
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p2

Sample Image

Mean and median in r boxplot

You can create the summary statistics beforehand and pass them through to geom_boxplot using stat = 'identity'

library(tidyverse)

div %>%
mutate(season = factor(season, level_order)) %>%
group_by(season, site) %>%
summarize(ymin = quantile(shannon, 0),
lower = quantile(shannon, 0.25),
median = median(shannon),
mean = mean(shannon),
upper = quantile(shannon, 0.75),
ymax = quantile(shannon, 1)) %>%
ggplot(aes(x = season, fill = site)) +
geom_boxplot(stat = 'identity',
aes(ymin = ymin, lower = lower, middle = mean, upper = upper,
ymax = ymax)) +
geom_point(aes(y = median, group = site),
position = position_dodge(width = 0.9)) +
xlab("season") +
ylab("Shannon index")

Sample Image



Related Topics



Leave a reply



Submit