简体   繁体   English

使用 ggsave 将多个 ggplot 图保存为相同的比例,尽管 x 轴文本具有不同的长度

[英]Save multiple ggplot figures as the same scale using ggsave, despite x axis text having different lengths

I am trying to create a set of multiple figures that are the same scale.我正在尝试创建一组相同比例的多个图形。 I have set the scale as the same for all figures, but when I save using ggsave , the figures save as different sizes because of the x-axis text lengths, making the scales not the same (ie while both go from 0-5, they are different sizes in the exported figure).我已将所有图形的比例设置为相同,但是当我使用ggsave保存时,由于 x 轴文本长度,图形保存为不同的大小,从而使比例不相同(即 go 从 0-5,它们在导出的图形中大小不同)。 I've tried assigning width and height in ggsave to counteract this, but the figures still export with different scale heights.我已经尝试在ggsave中分配宽度和高度来抵消这种情况,但这些数字仍然以不同的比例高度导出。 I cannot use facet_wrap or something similar because I am exporting these as transparent to overlay on a map.我不能使用facet_wrap或类似的东西,因为我将它们导出为透明的以覆盖在 map 上。

Here is an example of my data:这是我的数据示例:

table <- "Year Width_mm   Length_mm  Colour
1  1990     1.68 3.19 Blue
2  1990     1.47 2.88 Blue
3  1990     1.64 2.90 Blue
4  1990     1.80 3.20 Blue
5  2001     1.76 3.12 Blue
6  2001     1.61 3.11 Blue
7  2001     1.57 3.51 Blue
8  2001     1.48 3.54 Blue
9  2002     1.46 4.05 Green
10 2002     1.83 4.03 Green
11 2003-2004     1.83 4.11 Green
12 2003-2004     1.83 4.51 Green
13 2003-2004     1.83 4.12 Green
14 2005     1.83 3.55 Green
15 2005     1.83 3.57 Green
16 2005     1.83 3.55 Green"

#Create a dataframe with the above table
df <- read.table(text=table, header = TRUE)
df

Here is the code to create and export the figures:这是创建和导出图形的代码:

# Plot of length for blue
length.blue <- ggplot(df %>% filter(Colour=="Blue"), aes(x = Year, y = Length_mm)) + 
  geom_bar(stat="identity", position="dodge", fill = "#000000") +
  xlab("") + 
  ylab("") +
  scale_y_continuous(limits = c(0,5)) +
  #labs(fill = "") +
  theme_classic() +
  theme(legend.position = "") +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.text.x = element_text(size = 40, angle = 90),
        axis.text.y = element_text(size = 40),
        axis.line = element_line(size = 2),
        panel.background = element_rect(fill = "transparent", colour = NA),
        plot.background = element_rect(fill = "transparent", colour = NA)) 

length.blue

# Plot of length for green
length.green <- ggplot(df %>% filter(Colour=="Green"), aes(x = Year, y = Length_mm)) + 
  geom_bar(stat="identity", position="dodge", fill = "#000000") +
  xlab("") + 
  ylab("") +
  scale_y_continuous(limits = c(0,5)) +
  #labs(fill = "") +
  theme_classic() +
  theme(legend.position = "") +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.text.x = element_text(size = 40, angle = 90),
        axis.text.y = element_text(size = 40),
        axis.line = element_line(size = 2),
        panel.background = element_rect(fill = "transparent", colour = NA),
        plot.background = element_rect(fill = "transparent", colour = NA)) 

length.green

# Save figures
ggsave("length.blue.png", plot = length.blue, dpi=90, width = 5, height = 7)
ggsave("length.green.png", plot = length.green, dpi=90, width = 5, height = 7)

Edit:编辑:

Right now, this exports as:现在,这导出为: 电流输出

But I want it to export to something like this:但我希望它导出为这样的东西: 期望的输出

In ggplot, the panel size is determined by how much space is left after the titles and axes are drawn.在 ggplot 中,面板大小由绘制标题和轴后剩余的空间决定。 To get the panel sizes to match, you can build both plots, then copy the dimensions of the resulting gtable over from the green to the blue plot:要使面板大小匹配,您可以构建两个图,然后将生成的 gtable 的尺寸从绿色复制到蓝色 plot:

blue_plot <- ggplot_gtable(ggplot_build(length.blue))
blue_plot$heights <- ggplot_gtable(ggplot_build(length.green))$heights

ggsave("length.blue.png", plot = blue_plot, dpi=90, width = 5, height = 7)
ggsave("length.green.png", plot = length.green, dpi=90, width = 5, height = 7)

This results in这导致

length.blue.png length.green.png长度.blue.png 长度.green.png

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM