首页 > 解决方案 > 在 r 中,如何控制组合图中 (plot_grid) 中图与 x 轴的比例?

问题描述

我有两个情节,情节本身和 x 轴的文本比例很好。[在此处输入图像描述][1] 和 [在此处输入图像描述][2]

但是,当我使用 plot_grid 函数将它们组合成一个图时,x 轴的文本占组合图的更大比例。[在此处输入图片描述][3]

如何在保存或控制 x 轴与图本身的比例的同时组合图而不使文本变小?

这是我的代码:

  ggplot(variance_birds, aes(x=reorder(Species, order), precentage)) +
  geom_point(aes(shape=type), colour = 'black', size = 3, stroke = 1.5) +
  theme(axis.text.y = element_text(size = 18, colour = 'black'),
        axis.text.x = element_text(size = 14, angle = 70, colour = 'black', 
                                   vjust = 0.97,  hjust = 1),
        axis.title.y = element_text(size = 18, colour = 'black'),
        legend.title = element_blank(), legend.position = "none",
        legend.text = element_text(size=15),
        panel.background = element_rect(fill = 'white', colour = 'black'),
        panel.border = element_rect(colour = "black", fill=NA, size=.5))+
  scale_shape_manual("", values=c("mean"=20,
                              "mean_range"=17))+
  scale_y_continuous(breaks=c(0, 10, 20, 40, 60, 80, 100), limits = c(0, 100))+
  geom_segment(aes(x=Species,xend=Species, y=min,yend=max),inherit.aes=FALSE,
               linetype="longdash", color="black", size=0.55)+
  labs(x=NULL, y="Percentage of species-typical breeding units\nwith helpers in a population")+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  draw_label("Birds", x = 2.1, y = 100, size = 20, fontface="bold")


p_birds

##Plot mammals

p_mammals<-
  ggplot(variance_mammals, aes(x=reorder(Species, order), precentage)) +
  geom_point(aes(shape=type), colour = 'black', size = 3, stroke = 1.5) +
  theme(axis.text.y = element_text(size = 18, colour = 'black'),
        axis.text.x = element_text(size = 14, angle = 70, colour = 'black',
                                   vjust = 0.97,  hjust = 1),
        axis.title.y = element_text(size = 18, colour = 'black'),
        legend.title = element_blank(), legend.position = "none",
        legend.text = element_text(size=15),
        panel.background = element_rect(fill = 'white', colour = 'black'),
        panel.border = element_rect(colour = "black", fill=NA, size=.5))+
  scale_shape_manual("", values=c("mean"=20,
                                  "mean_range"=17))+
  scale_y_continuous(breaks=c(0, 10, 20, 40, 60, 80, 100), limits = c(0, 100))+
  geom_segment(aes(x=Species,xend=Species, y=min,yend=max),inherit.aes=FALSE,
               linetype="longdash", color="black", size=0.55)+
  labs(x=NULL, y="Percentage of species-typical breeding units\nwith helpers in a population")+
  theme(plot.margin=unit(c(1,1,1,1),"cm"))+
  draw_label("Mammals", x = 2.9, y = 92, size = 20, fontface="bold")

p_mammals+
  draw_label("'", x = 14.3, y = -30.5, angle=70, size = 20, colour="black")+
  coord_cartesian(ylim = c(0, 100), clip = "off")


#combining the two plots

combined_plot<-plot_grid(p_mammals+ theme(legend.position = 'none'),
                        p_birds+ theme(legend.position = 'none'),
                       align = "h", ncol = 1, rel_heights = c(5/10, 5/10))

combined_plot```

Thanks!

  [1]: https://i.stack.imgur.com/FOFn6.jpg
  [2]: https://i.stack.imgur.com/sf7In.jpg
  [3]: https://i.stack.imgur.com/h1s8V.jpg

标签: rggplot2plot

解决方案


实际上,添加这是最简单的解决方案: ggsave("Figure 2.pdf", width = 40, height = 50, units = "cm") 感谢您的帮助!


推荐阅读