英文:
Save ggplot2 Image Proportionally to X axis
问题
我正在尝试在ggplot2中准备一些图像,其中x轴在我的每个图中都有所变化。例如,图1的X轴跨度为200个单位,图2的X轴跨度为300个单位,依此类推。我希望保存这些图像,以使不同图像的X轴单位成比例且直接可比。有人知道如何做到这一点吗?当我使用ggsave时,它们都以相同的长度保存,而不考虑x轴单位。
英文:
I'm trying to prepare a few images in ggplot2 where the x-axis varies for each one of my plots. For example, plot 1 has an X-axis that spans 200 units, plot 2 has an X-axis that spans 300 units etc. I would like to save the images so that X axis units for my different images are all proportional and directly comparable to each other. Does anyone know how to do this? When I use ggsave, they all save as the same length, regardless of x-axis units.
答案1
得分: 0
你可以尝试将ggsave的宽度设置为x轴总跨度的函数。例如,以下代码将导致200单位图的宽度为10厘米,300单位图的宽度为15厘米。
width_plot <- 0.05 * (max(data$x) - min(data$x))
ggsave("myplot.jpeg",
plot = current_plot,
device = "jpeg",
width = width_plot,
height = 8,
units = "cm",
dpi = 300)
英文:
You could try setting the ggsave width as a function of the x-axis total span. For example, the following code will result in a plot with a 10-cm width for the 200 units plot and a 15-cm width for the 300 units plot.
width_plot <- 0.05 * (max(data$x) - min(data$x))
ggsave("myplot.jpeg",
plot = current_plot,
device = "jpeg",
width = width_plot,
height = 8,
units = "cm",
dpi = 300)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论