为ggplot2生成的多个图添加标题和格式化Y轴标签
问题描述:
我有一个多槽图,其中包含使用ggplot2生成的10个散点图。我用来创建情节的代码已经从这里解除R cookbook.我的问题是我想为每个散点图添加不同的标题,例如,情节1标题可以标题为“情节1”,而情节2可以标题为“情节2”等等等等。我也想将标签从当前标签“Y”更改为所有地块的“购买”。为ggplot2生成的多个图添加标题和格式化Y轴标签
答
只需创建您的图和标题每个人作为您引用的代码。然后安排使用gridExtra
包。 ggtitle
做标题,ylab
函数可以用于y标签。
library(ggplot2)
# This example uses the ChickWeight dataset, which comes with ggplot2
# First plot
p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
geom_line() +
ggtitle("Growth curve for individual chicks")
# Second plot
p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
geom_point(alpha=.3) +
geom_smooth(alpha=.2, size=1) +
ggtitle("Fitted growth curve per diet")
# Third plot
p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
geom_density() +
ggtitle("Final weight, by diet")
# Fourth plot
p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
geom_histogram(colour="black", binwidth=50) +
facet_grid(Diet ~ .) +
ggtitle("Final weight, by diet") +
theme(legend.position="none") # No legend (redundant in this graph)
require(gridExtra)
grid.arrange(p1, p2, p3, p4, nrow = 2)
+0
非常感谢 –
在此处发布您的代码 – andriatz
使用'sprintf'或'paste'在每次迭代中调用'labs'来创建标签。 – ulfelder