简体   繁体   English

R 多个绘图,具有通用 y 轴缩放和共享 x 轴 label

[英]R multiple plots with common y-axis scaling and shared x-axis label

this is my first question here.这是我在这里的第一个问题。 Please be nice to a (R-) newbie:)请善待(R-)新手:)

I'm trying to arrange multiple plots from different data frames, so that they share a common y-axis and the x-axis label/numbers are only displayed under the bottom plot.我正在尝试从不同的数据框中排列多个图,以便它们共享一个共同的 y 轴,并且 x 轴标签/数字仅显示在底部 plot 下方。

My data frames consist of the following columns: measurement time ( messzeitpunkt_s ), mean ( z_mean ), upper limit of the confidence interval ( z_ci_up ), lower limit of the confidence interval ( z_ci_low ).我的数据框由以下列组成:测量时间( messzeitpunkt_s )、平均值( z_mean )、置信区间的上限( z_ci_up )、置信区间的下限( z_ci_low )。

This is what I've tried:这是我尝试过的:

  1. I created the plots I want to stack over another:我创建了要堆叠在另一个上的图:

` `

p_neutral <- ggplot(neutral_all_mean_ci, aes(messzeitpunkt_s, z_mean, colour = " Mittelwert ")) +
  theme_bw()+
  geom_ribbon(aes(messzeitpunkt_s, ymax = z_ci_up, ymin = z_ci_low), fill = "#FCE5D7", alpha=0.8) +
  labs(x = "Messzeit [s]",y = "Neutral")+
  geom_line() +
  geom_line(aes(y = z_ci_low, colour =" 95% CI ")) +
  geom_line(aes(y = z_ci_up, colour = " 95% CI ")) +
  theme(legend.position = "none") +
  scale_color_manual(values=c("#F7B383", "#3074B3", "#F7B383"))

` `

I've used the same code to create the plots p_anger, p_disgust, p_fear, p_happiness, p_sadness, p_surprise , only by replacing the data frame neutral_all_mean_ci in the ggplot-function with anger_all_mean_ci and so on and changing the respective labels.我使用相同的代码来创建绘图p_anger、p_disgust、p_fear、p_happiness、p_sadness、p_surprise ,仅通过用anger_all_mean_ci等替换 ggplot 函数中的数据框neutral_all_mean_ci等并更改各自的标签。

  1. When I stack them over another using ggarrage() :当我使用ggarrage()将它们堆叠在另一个上时:

ggarrange(p_neutral, p_anger, p_disgust, p_fear, p_happiness, p_sadness, p_surprise, ncol=1)

I end up with this plot .我最终得到了这个plot

Now I would like to have a common y-axis scale (so the plots show the same range) and the x-axis label/numbers only at the bottom plot.现在我想有一个共同的 y 轴刻度(所以图显示相同的范围)和 x 轴标签/数字仅在底部 plot。

Can someone please help me out?有人可以帮我吗? Thanks in advance!提前致谢!

Welcome to StackOverflow!欢迎来到 StackOverflow!

I think you can use the scale_y_continuous(limits = c(-0.5, 1.5)) in all plots and labs(x="Messzeit [s]") only in the last geom_line() , like this:我认为您只能在最后一个geom_line()中使用scale_y_continuous(limits = c(-0.5, 1.5))labs(x="Messzeit [s]") ,如下所示:

p_neutral <- ggplot(neutral_all_mean_ci, aes(messzeitpunkt_s, z_mean, colour = " Mittelwert ")) +
  theme_bw()+
  geom_ribbon(aes(messzeitpunkt_s, ymax = z_ci_up, ymin = z_ci_low), fill = "#FCE5D7", alpha=0.8) + scale_y_continuous(limits = c(-0.5, 1.5)) +
  labs(x = "",y = "Neutral")+ scale_y_continuous(limits = c(-0.5, 1.5)) +
  geom_line() + labs(x="") +
  geom_line(aes(y = z_ci_low, colour =" 95% CI ")) + scale_y_continuous(limits = c(-0.5, 1.5)) + labs(x="") +
  geom_line(aes(y = z_ci_up, colour = " 95% CI ")) + scale_y_continuous(limits = c(-0.5, 1.5)) + labs(x="Messzeit [s]") +
  theme(legend.position = "none") + 
  scale_color_manual(values=c("#F7B383", "#3074B3", "#F7B383"))

Let me know if this could help you because I couldn't run it without your dataset.让我知道这是否可以帮助您,因为没有您的数据集我无法运行它。

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

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