我有兩個 ggplots,我希望灰色區域的大小完全相等。
現在通過玩耍,我可以通過將第一個繪圖寬度設定為 12 并將第二個繪圖寬度設定為 14 來非常接近。
ggsave(filename="test1.png", myPlot, width = 12, height =12, dpi=300, units="cm", device='png')
ggsave(filename="test2.png", myPlot, width = 14, height =12, dpi=300, units="cm", device='png')

我的問題是我是否可以從第一個圖中提取一些變數,以便我可以自動計算第二個圖的正確寬度(即,使灰色區域大小相同的寬度)。
uj5u.com熱心網友回復:
除了 Allan 已經給出的選項之外,您可能還會發現ggh4x::force_panelsizes()為繪圖設定大小很方便(免責宣告:我寫的)。
library(ggplot2)
a <- ggplot(mpg, aes(displ, hwy, colour = cyl))
geom_point()
b <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species))
geom_point()
guides(y.sec = "axis")
a ggh4x::force_panelsizes(rows = unit(4, "cm"), cols = unit(5, "cm"))

b ggh4x::force_panelsizes(rows = unit(4, "cm"), cols = unit(5, "cm"))

由
使用reprex v2.0.2創建于 2022-11-04
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527517.html
標籤:rggplot2
上一篇:如何在R中為三元圖設定影片
