简体   繁体   English

与 ggarrange 相同的 y 轴比例

[英]Same y-axis scale with ggarrange

I would like to use ggarrange to combine two plots into a single one, and use the same scaling for the y-axis to make it easier to compare the values, in my case, temperature calculations.我想使用 ggarrange 将两个图合并为一个图,并为 y 轴使用相同的缩放比例,以便更轻松地比较这些值,在我的情况下是温度计算。 This is my sample data:这是我的示例数据:

    Sample  Group   Temperature_A   Temperature_B
1   Sample01    A   20  34
2   Sample02    B   21  31
3   Sample03    A   25  35
4   Sample04    A   18  27
5   Sample05    B   19  29
6   Sample06    A   22  32
7   Sample07    B   23  33
8   Sample08    B   17  28
9   Sample09    A   15  26
10  Sample10    B   28  39

Using the following code, I get the following plot:使用以下代码,我得到以下图:

p1 <- ggplot(df, aes(Group, Temperature_A)) +
 geom_boxplot()

p2 <- ggplot(df, aes(Group, Temperature_B)) +
 geom_boxplot()

plate <- ggarrange(p1, p2, align = "hv")

plate

样本图

What I would like to have is for them to share their y-axis so that I can easily compare the two temperature calculation methods.我想要的是让他们共享他们的 y 轴,以便我可以轻松比较两种温度计算方法。 I can do this manually with + scale_y_continuous(limits = c(a,b)) where I set the appropriate values for a and b, however, I have a lot of different dataframes with different temperature ranges.我可以使用+ scale_y_continuous(limits = c(a,b))手动执行此操作,其中我为 a 和 b 设置了适当的值,但是,我有很多具有不同温度范围的不同数据帧。 Ideally, I would use the lowest value (+ some space) from both plots, and the highest value (+ some space) from both plots for the limits of both plots.理想情况下,我会使用两个图中的最低值(+ 一些空间),并使用两个图的最高值(+ 一些空间)作为两个图的限制。 Is there a way to achieve this?有没有办法实现这一目标?

My manual approach and the desired output:我的手动方法和所需的输出:

p1 <- ggplot(df, aes(Group, Temperature_A)) +
 geom_boxplot() + 
 scale_y_continuous(limits = c(10, 40))

p2 <- ggplot(df, aes(Group, Temperature_B)) +
 geom_boxplot() + 
 scale_y_continuous(limits = c(10, 40))

plate <- ggarrange(p1, p2, align = "hv")

plate

期望的输出

This is just to illustrate how you could use facets.这只是为了说明如何使用facet。 Because you have two columns for temperature, this is called the 'wide format'.因为你有两列温度,这被称为“宽格式”。 If you reshape the data to the long format, you can easily use facets.如果将数据重塑为长格式,则可以轻松使用 facet。

library(ggplot2)

df <- read.table(text = "    Sample  Group   Temperature_A   Temperature_B
1   Sample01    A   20  34
2   Sample02    B   21  31
3   Sample03    A   25  35
4   Sample04    A   18  27
5   Sample05    B   19  29
6   Sample06    A   22  32
7   Sample07    B   23  33
8   Sample08    B   17  28
9   Sample09    A   15  26
10  Sample10    B   28  39", header = TRUE)

# Reshape
long <- tidyr::pivot_longer(
  df, c("Temperature_A", "Temperature_B"), 
  values_to = "Temperature", names_to = "Temp_name"
)

# Example of facets
ggplot(long) +
  geom_boxplot(aes(Group, Temperature)) +
  facet_wrap(~ Temp_name)

Created on 2021-07-27 by the reprex package (v1.0.0)reprex 包(v1.0.0) 于 2021 年 7 月 27 日创建

I agree with teunbrand.我同意 teunbrand 的观点。 Here is an slightly modified approach:这是一个稍微修改的方法:

  1. bring data in long format以长格式导入数据
  2. ignore outlieres outlier.shape = NA忽略异常值outlier.shape = NA
  3. adapt ylim and适应ylim
  4. facet_wrap
library(tidyverse)
df1 <- df %>% 
    pivot_longer(
        cols = starts_with("Temperature"), 
        names_to = "Temperature",
        values_to = "values"
    ) 


ggplot(df1, aes(Group, values)) +
    geom_boxplot(outlier.shape = NA) +
    coord_cartesian(ylim = c(10, 40)) +
    facet_wrap(~Temperature) + 
    theme_bw()

在此处输入图片说明

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

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