r - 如何在ggplot2中指定图形的大小而与轴标签无关

标签 r ggplot2 grid dimensions

说我有一个数据框,想画一个图

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")

enter image description here

问题是我想指定图本身的尺寸,但独立于轴标签,以便下面指定的图在轴的绝对长度方面具有相同的大小和尺寸。
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")

enter image description here

有没有一种方法可以轻松做到这一点?

这是一个在轴长方面更接近我想要的图的示例:

enter image description here

唯一的问题是,通过更改长宽比,我最终挤压y轴以使x轴的长度更接近偶数...

最佳答案

使用ggplotGrob。像这样的东西:

g1 <- ggplot(...)
g2 <- ggplot(...)

g1grob <- ggplotGrob(g1)
g2grob <- ggplotGrob(g2)

grid.arrange(g1grob, g2grob)

关于r - 如何在ggplot2中指定图形的大小而与轴标签无关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46840724/

相关文章:

java - Nebula Grid 的多行工具提示

html - 为什么 col-xs-0 占据了 100% 的行宽?

r - 在r中按组绘制散点图

r - 如何在 R 中的 h2o.grid 中调整 hidden_​​dropout_ratios

r - 每个单位有轮廓的条形图?

r - 使图例不可见,但保持图形尺寸和边距相同

r - tidytext——如何做共性和对比词云

r - stat_密度2d : removed rows containing non-finite values

r - ggplot中的控制点边框厚度

wpf - 在模板中定义的网格之间共享列宽