我使用下面给出的代码使用ggplot在同一页面上绘制多个图形。而不是每个图形都有自己的xlabel,ylabel,标题和图例描述,我只希望每个页面都有一个这样的图形。我试图找到解决方案,但无济于事。有人可以让我知道,还是指向相关主题?感谢。
plots <- dlply(dacc, .(SIC), function(x){
ggplot(x, aes(x = PER)) +
geom_line(aes(y = MEAN, group = NLEAD, color = NLEADMEAN)) +
geom_line(aes(y = MED, group = NLEAD, color = NLEADMED)) +
xlab("(Total Asset Percentile Cutoff To Define Big Firms)") +
ylab("Mean Absolute Discretioary Accrual For Different Groups") +
ggtitle(paste("Mean ADA Across Groups For SIC = ", unique(x$SIC))) +
ylim(0, ymax/2)
})
ml <- do.call(marrangeGrob, c(plots, list(nrow = 2, ncol = 2)));
ggsave("my_plots.pdf", ml, height = 7, width = 13, units = "in");
编辑:
ggplot(dacc, aes(x = PER)) +
geom_line(aes(y = MEAN, group = NLEAD, color = NLEADMEAN)) +
geom_line(aes(y = MED, group = NLEAD, color = NLEADMED)) +
xlab("(Total Asset Percentile Cutoff To Define Big Firms)") +
ylab("Mean Absolute Discretioary Accrual For Different Groups") +
ggtitle(paste("Mean ADA Across Groups For SIC = ", unique(x$SIC))) +
ylim(0, ymax/2) +
facet_wrap(~SIC,nrow=2)
这看起来似乎更好,但我遇到了这个问题的多个问题。 (1)在终端上,我能够看到图表,它只显示了xlab和ylab。但是如何保存图表呢? (2)我有大约50个SIC,所以有50个grpahs,所以我假设当我保存时,假设nrow = 10,我应该能够在每个页面上看到xlab和ylab。
答案 0 :(得分:2)
如评论所述,您应该在此处使用分面功能。例如:
facet_wrap(~SIC,nrow=2)
你的代码变成这样:
ggplot(dacc, aes(x = PER)) +
geom_line(aes(y = MEAN, group = NLEAD, color = NLEADMEAN)) +
geom_line(aes(y = MED, group = NLEAD, color = NLEADMED)) +
xlab("(Total Asset Percentile Cutoff To Define Big Firms)") +
ylab("Mean Absolute Discretioary Accrual For Different Groups") +
ggtitle(paste("Mean ADA Across Groups For SIC = ", unique(x$SIC))) +
ylim(0, ymax/2) +
facet_wrap(~SIC,nrow=2)