【发布时间】:2016-08-18 01:50:55
【问题描述】:
使用diamonds,我想为cut 的4 个级别(Fair、Good、Very Good 和Premimum)绘制carat 与price。
我没有让facet_wrap()控制坐标轴的中断,而是制作了四个图来控制坐标轴的中断。
library(ggplot2)
library(egg)
library(grid)
f1 <-
ggplot(diamonds[diamonds$cut=="Fair",], aes(carat, price))+
geom_point()+
facet_wrap(~cut, ncol =2)+
scale_x_continuous(limits = c(0,4), breaks=c(0, 1, 2, 3, 4))+
scale_y_continuous(limits = c(0,10000), breaks=c(0, 2500, 5000, 7500, 10000))+
labs(x=expression(" "),
y=expression(" "))
f2 <-
ggplot(diamonds[diamonds$cut=="Good",], aes(carat, price))+
geom_point()+
facet_wrap(~cut, ncol =2)+
scale_y_continuous(limits = c(0,5000), breaks=c(0, 1000, 2000, 3000, 4000, 5000))+
labs(x=expression(" "),
y=expression(" "))
f3 <-
ggplot(diamonds[diamonds$cut=="Very Good",], aes(carat, price))+
geom_point()+
facet_wrap(~cut, ncol =2)+
scale_x_continuous(limits = c(0,1), breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1))+
scale_y_continuous(limits = c(0,1000), breaks=c(0, 200, 400, 600, 800, 1000))+
labs(x=expression(" "),
y=expression(" "))
f4 <-
ggplot(diamonds[diamonds$cut=="Premium",], aes(carat, price))+
geom_point()+
facet_wrap(~cut, ncol =2)+
scale_x_continuous(limits = c(0,1.5), breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4))+
scale_y_continuous(limits = c(0, 3000), breaks=c(0, 500, 1000, 1500, 2000, 2500, 3000))+
labs(x=expression(" "),
y=expression(" "))
fin_fig <- ggarrange(f1, f2, f3, f4, ncol =2)
fin_fig
结果
每个图都有一系列不同的 y 值
问题
在所有方面,x 和 y 轴都是相同的。唯一的区别是最小值、最大值和休息时间。我想在这个图中添加 x 和 y 标签。我可以在任何 Word 文档或图像编辑器中手动执行此操作。反正有没有直接在R中做?
【问题讨论】:
-
看看
grid::textGrob和ggplot2::annotation_custom -
您可以使用
gridExtra,它具有left和bottom参数。 :p <- ggplot() ; g <- gridExtra::arrangeGrob(p,p,p,p,ncol=2, bottom=grid::textGrob("bottom lab"), left=grid::textGrob("left lab", rot=90)) ; grid::grid.newpage() ; grid::grid.draw(g)