Plot stat_summary
points with full opacity over box plots with variable alpha.
################################################################################
# !Important!
# Assume that `palette1` is already defined, and shared between multiple figures
################################################################################
ncols = length(unique(mtcars$gear))
palette1 = scales::hue_pal()(ncols)
names(palette1) = sort(unique(mtcars$gear))
p = ggplot(mtcars, aes(x=as.factor(gear), y=mpg,fill=as.factor(gear),alpha=as.factor(cyl))) +
geom_boxplot() +
scale_alpha_discrete(range = c(0.1, 0.8),guide = guide_legend(override.aes = list(fill = "black"))) +
rotate_x_text(45) +
facet_grid(~gear,scale="free") +
stat_summary(fun=mean, geom="point", shape=5, size=5,position = position_dodge(.75),stroke=1.5)
set_palette(p, palette1)
The reduced opacity of stat_summary
points makes them hard to see.
This answer implements corrections suggested by @stefan in comments to the original post.
p2 = ggplot(mtcars,aes(x=as.factor(gear), y=mpg,fill=as.factor(gear))) +
geom_boxplot(aes(x=as.factor(gear), y=mpg,fill=as.factor(gear),alpha=as.factor(cyl))) +
scale_alpha_discrete(range = c(0.1, 0.8),guide = guide_legend(override.aes = list(fill = "black"))) +
rotate_x_text(45) +
facet_grid(~gear,scale="free") +
stat_summary(fun=mean, geom="point", shape=18, size=5,
aes(group=as.factor(cyl),x=as.factor(gear), y=mpg),
position = position_dodge(.75),stroke=1.5)
set_palette(p2, palette1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.