英文:
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")
我想实际上将这些数字以确切的大小放入图中(而不减小它们的大小)。我尝试通过操纵ylim
参数来扩展y轴的范围,并在数据集的最大值上添加一些数字ylim(0, max(data$values)+5)
。问题是,R在分面之前考虑了最大值,所以它不是面部特定的,结果不是我想要的:
有没有办法单独为各个分面操纵和扩展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 <- data.frame(category = c("green", "red", "blue", "yellow"),
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 = "t.test", aes(label = ..p.value..), size = 5,
comparisons = list(c("A" , "C"), c("A" , "D"))) +
facet_wrap( ~ category, scales = "free_y")
That results in this kind of graph, where top p-values don't fit the plot.
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:
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")
英文:
Here is as an alternative approach adding extra space to max value:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论