将 ggplot2 图像按X轴等比例保存

huangapple go评论132阅读模式
英文:

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 &lt;- 0.05 * (max(data$x) - min(data$x))
ggsave(&quot;myplot.jpeg&quot;,
       plot = current_plot,
       device = &quot;jpeg&quot;,
       width = width_plot,
       height = 8,
       units = &quot;cm&quot;,
       dpi = 300)

huangapple
  • 本文由 发表于 2020年1月6日 02:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定