我正在使用下面的代码生成一个简单的箱线图ggplot2
:
# Libs data
data("mtcars"); require(ggplot2); require(ggthemes)
# Chart
ggplot(data = mtcars) +
geom_boxplot(aes(y = wt, x = as.factor(am)),
fill = "gray87") +
xlab("AM") +
ylab("WT") +
theme_gdocs() +
ggtitle("WT by AM") +
theme(axis.title.y = element_text(angle = 90),
axis.ticks = element_line(colour = "black", linetype = "solid",
size = 0.5),
panel.grid = element_line(colour = "gray"))
任务
我想为我的图表添加一个副标题,并对它的呈现方式进行一些控制。我正在关注这个讨论并使用代码:
# Chart
ggplot(data = mtcars) +
geom_boxplot(aes(y = wt, x = as.factor(am)),
fill = "gray87") +
xlab("AM") +
ylab("WT") +
theme_gdocs() +
ggtitle(expression(atop("WT by AM",
atop(italic("Some crucial note that has to be here"), "")))) +
theme(axis.title.y = element_text(angle = 90),
axis.ticks = element_line(colour = "black", linetype = "solid",
size = 0.5),
panel.grid = element_line(colour = "gray"))
这看起来真的很糟糕,我想改变一些事情:
- 使副标题和标题左对齐
- 减少两行之间的空白
- 保持字体粗体
尝试
我尝试了不同的东西,例如下面的代码:
ggplot(data = mtcars) +
geom_boxplot(aes(y = wt, x = as.factor(am)),
fill = "gray87") +
xlab("AM") +
ylab("WT") +
theme_gdocs() +
ggtitle(expression(atop("WT by AM",
atop(italic("Stupid note"), "")))) +
theme(axis.title.y = element_text(angle = 90),
axis.ticks = element_line(colour = "black", linetype = "solid",
size = 0.5),
panel.grid = element_line(colour = "gray"),
plot.title = element_text(size = 16, colour = "black", hjust = -1))
但它完全隐藏了标题: