如何在ggplot2中单独控制和增加不同分面的Y轴范围

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

How to control and increase y axis range individually for different facets in ggplot2

问题

我搜索了解决方案,因为这似乎是一个经常被提问的问题,但找不到答案,所以我会在这里提问。我的示例使用了ggpubr统计包创建的标签,但问题更通用。

假设我有这个数据集,我试图绘制它并标记t-test的p值来比较组,如下所示:

library(ggplot2)
library(ggpubr)
set.seed(42)

data <- data.frame(category = c("green", "red", "blue", "yellow"), 
                   type = sample(LETTERS[1:4], 100, replace = TRUE),
                   values = c(1, 2, 50, 5) * runif(100))

ggplot(data, aes(x = type, y = values, color = type)) +
  geom_point(size = 3) +
  stat_compare_means(method = "t.test", aes(label = ..p.value..), size = 5, 
                     comparisons = list(c("A" , "C"), c("A" , "D"))) +
  facet_wrap( ~ category, scales = "free_y")

结果是这种类型的图表,其中顶部的p值不适合图表。
如何在ggplot2中单独控制和增加不同分面的Y轴范围

我想实际上将这些数字以确切的大小放入图中(而不减小它们的大小)。我尝试通过操纵ylim参数来扩展y轴的范围,并在数据集的最大值上添加一些数字ylim(0, max(data$values)+5)。问题是,R在分面之前考虑了最大值,所以它不是面部特定的,结果不是我想要的:

如何在ggplot2中单独控制和增加不同分面的Y轴范围

有没有办法单独为各个分面操纵和扩展ylim,或者有其他方法来适应这些标签?此外,为什么分面不仅对其工作的数据集部分考虑最大值,以及在这种情况下如何为数据集的一部分提供分面值?

谢谢!

英文:

I searched for the solution as it sounds like a frequent question, but couldn't find the answer, so will ask it here. My example is using labels created by ggpubr statistical package, but the question is more general.

Let's say I have this dataset and I'm trying to plot it and mark t-test p-value to compare the groups like that:

library(ggplot2)
library(ggpubr)
seed(42)

data &lt;- data.frame(category = c(&quot;green&quot;, &quot;red&quot;, &quot;blue&quot;, &quot;yellow&quot;), 
                  type = sample(LETTERS[1:4], 100, replace = T),
                  values = c(1, 2, 50, 5) * runif(100))

ggplot(data, aes(x = type, y = values, color = type)) +
 geom_point(size = 3) +
 stat_compare_means(method = &quot;t.test&quot;, aes(label = ..p.value..), size = 5, 
                    comparisons = list(c(&quot;A&quot; , &quot;C&quot;), c(&quot;A&quot; , &quot;D&quot;))) +
 facet_wrap( ~ category, scales = &quot;free_y&quot;)

That results in this kind of graph, where top p-values don't fit the plot.
如何在ggplot2中单独控制和增加不同分面的Y轴范围

I'd like to actually fit those numbers in the plot in this exact size (without decreasing their size).
I tried that by manipulating ylim parameter by expanding range of y-axis and adding some number to the maximum value in the dataset ylim(0, max(data$values)+5). The problem, that R considers that max value before facetting, so it's not facet-specific and results in what I mean to get:

如何在ggplot2中单独控制和增加不同分面的Y轴范围

Is there any way to manipulate and expand ylim individually for facets or any other way to fit in those labels ?
Also, why facetting doesn't take max value only for the part of dataset it's working with and how to provide the values for a part of dataset for facetting in this case ?

Thank you!

答案1

得分: 1

这是一种添加额外空间到最大值的替代方法的代码:

library(dplyr)
library(ggplot2)
library(ggpubr)

data %>%
  mutate(values_adj = ifelse(values == max(values), values + 10, values), .by = category) %>%
  ggplot(aes(x = type, y = values_adj, color = type)) +
  geom_point(size = 3) +
  stat_compare_means(method = "t.test", aes(label = ..p.value.., y = values), size = 5, 
                     comparisons = list(c("A", "C"), c("A", "D"))) +
  facet_wrap( ~ category, scales = "free_y")

如何在ggplot2中单独控制和增加不同分面的Y轴范围

英文:

Here is as an alternative approach adding extra space to max value:

library(dplyr)
library(ggplot2)
library(ggpubr)

data %&gt;%
  mutate(values_adj = ifelse(values == max(values), values + 10, values), .by = category) %&gt;% 
  ggplot(aes(x = type, y = values_adj, color = type)) +
  geom_point(size = 3) +
  stat_compare_means(method = &quot;t.test&quot;, aes(label = ..p.value.., y = values), size = 5, 
                     comparisons = list(c(&quot;A&quot; , &quot;C&quot;), c(&quot;A&quot; , &quot;D&quot;))) +
  facet_wrap( ~ category, scales = &quot;free_y&quot;)

如何在ggplot2中单独控制和增加不同分面的Y轴范围

huangapple
  • 本文由 发表于 2023年7月11日 03:15:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656676.html
匿名

发表评论

匿名网友

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

确定