ggplot2--Titles(设置标题)

标签:
ggplot2titlesggplot2标题杂谈 |
分类: ggplot2 |
本节主要涉及到如何在ggplot2中设置或修改标题(Titles)
解决方案:
画一个没有标题的图, R代码如下:
stopifnot(require(ggplot2))
bp = ggplot(PlantGrowth, aes(x=group, y=weight)) +
geom_boxplot(fill="grey")
# 打开画图结果(或bp)
print(bp)
添加标题(title)
bp + opts(title="Plant growth")
# If the title is long, it can be split into multiple lines
with \n
bp + opts(title="Plant growth with\ndifferent
treatments")
# Reduce line spacing and use bold text
bp + opts(title="Plant growth with\ndifferent treatments")
+
opts(plot.title = theme_text(size=14, lineheight=.8,
face="bold"))
写成一行如下, 同时添加xlab和ylab:
ggplot(PlantGrowth, aes(x=group, y=weight)) +
geom_boxplot(fill="grey") + opts(title="Plant growth\ndifferent
treatment") + xlab("Growth") + ylab("Weight")
在R命令上中通过help(opts)查看有关opts更多的帮助和示例.
Reference