说我有一个数据框并想作图
df <- melt(iris)
p <- ggplot(data = df, aes(x = Species, y = value)) +
geom_boxplot() + theme(aspect.ratio = 1)
然后,我想使用ggsave()保存此图的pdf
ggsave(plot = p, width = 3, height = 3, dpi = 300, filename = "not squished axis.pdf")
问题是我想指定图本身的尺寸,但独立于轴标签,以便下面指定的图在轴的绝对长度方面具有相同的大小和尺寸。
q <- ggplot(data = df, aes(x = Species, y = (value)*10000000)) +
geom_boxplot() + theme(aspect.ratio = 1)
ggsave(plot = q, width = 3, height = 3, dpi = 300, filename = "squished axis.pdf")
有没有一种方法可以轻松做到这一点?
这是一个在轴长方面更接近我想要的图的示例:
唯一的问题是,通过更改长宽比,我最终挤压了y轴,使x轴的长度更接近均匀。
使用ggplotGrob
。像这样:
g1 <- ggplot(...)
g2 <- ggplot(...)
g1grob <- ggplotGrob(g1)
g2grob <- ggplotGrob(g2)
grid.arrange(g1grob, g2grob)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句