使用diamonds
的網格,我想繪製carat
VS price
爲cut
4級(Fair
,Good
,Very Good
和Premimum
)。ggplot:如何常見的X和Y標籤添加到地塊
我沒有讓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
RESULT
每個情節具有一定範圍的不同的y值
QUESTION
在所有方面中,x軸和y軸的 是相同的。唯一的區別是最小,最大和休息。我想爲此圖添加x和y標籤。我可以在任何文檔或圖像編輯器中手動執行此操作。無論如何要直接在R中執行它?
看看'網:: textGrob'和'GGPLOT2 :: annotation_custom' – shayaa
你可以使用'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)' – user20650