废话
ggplot2
是R语言绘图神奇,在各种扩展包的加持下无往而不胜。然而使用ggplot2
绘制boxplot的时候,生成的图片看着总有一丝丝的别扭。
比较一下教科书中的boxplot示意图:
ggplot2
的boxplot的上下边缘的横线去哪了?
虽然看久了也就习惯了,但是也不是没有办法把这2条横线加回来。办法是使用stat_boxplot()
函数
样图
代码
library(ggplot2)
library(wesanderson)
pal <- wes_palette("Royal1")
ggplot(dt, aes(x = dataset, y = contacts)) +
geom_boxplot(aes(color = dataset, fill = dataset),
width = .7, size = .3, alpha = .5, outlier.size = .1, ) +
stat_boxplot(geom = "errorbar",
aes(ymin = ..ymax.., color = dataset),
width = 0.2, size = .3) +
stat_boxplot(geom = "errorbar",
aes(ymax = ..ymin.., color = dataset), width = 0.2, size = .3) +
theme_classic() +
theme(
axis.text.x = element_text(size = 8, face = "bold", angle = 50, hjust = 1),
axis.text.y = element_text(size = 8, face = "bold"),
axis.title = element_text(size = 10, face = "bold"),
plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
plot.subtitle = element_text(size = 10, face = "bold", hjust = 0.5),
) +
labs (x = "", y = "", title = "Total contacts", subtitle = "(chr3)") +
scale_fill_manual(values = pal) +
scale_color_manual(values = pal) +
guides(fill = "none", color = "none")