英文:
Keeping the Scale_x_discrete tick labels in ggplot after facet_grid
问题
我试图在facet_grid之后添加一个新的x轴刻度标签,但似乎不像我希望的那样工作。希望这里有人经历过类似情况并知道解决方法。
为了使代码可复制,我正在使用mpg数据集,希望每个人都能理解问题。
使用下面的代码,我正在将刻度轴标签更改为每个类别的汽车总数:
library(tidyverse)
library(ggpubr)
p3 <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2, 2.5)) %>%
ggplot(aes(x = class, y = hwy, col = class)) +
geom_violin() +
geom_point() +
#facet_grid(cols = vars(displ))+
stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # Add statistical comparisons
theme_bw() +
scale_x_discrete(labels = function(x) {
num_cars <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2, 2.5)) %>%
filter(class %in% x) %>%
group_by(class) %>%
summarise(total_cars = n()) %>%
mutate(label = total_cars)
num_cars$label
})
p3
这将创建以下图表:
当我将图表分开以便按displ分组时,x轴不再起作用,尽管统计数据仍然有效。
p3 <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2, 2.5)) %>%
ggplot(aes(x = class, y = hwy, col = class)) +
geom_violin() +
geom_point() +
facet_grid(cols = vars(displ)) +
stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # Add statistical comparisons
theme_bw() +
scale_x_discrete(labels = function(x) {
num_cars <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2, 2.5)) %>%
filter(class %in% x) %>%
group_by(class) %>%
summarise(total_cars = n()) %>%
mutate(label = total_cars)
num_cars$label
})
p3
结果如下:
但我希望刻度标签为2、3、2、2。
希望有人能对此有一个简单的答案(我确信这很简单)。
干杯!
英文:
I am trying to add a new x tick label after facet_grid and it does not seem to work as I wished. Hopefully someone here has gone through the same and knows the trick.
To make the code reproducible I am using the mpg dataset and hopefully everyone can understand the question.
With the code below I am changing the tick axis label to the total number of cars per category:
library(tidyverse)
library(ggpubr)
p3 <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2,2.5)) %>%
ggplot(aes(x=class, y=hwy, col=class))+
geom_violin()+
geom_point()+
#facet_grid(cols = vars(displ))+
stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # Add statistical comparisons
theme_bw()+
scale_x_discrete(labels = function(x) {
num_cars <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2,2.5)) %>%
filter(class %in% x) %>%
group_by(class) %>%
summarise(total_cars = n())%>%
mutate(label = total_cars)
num_cars$label
})
p3
This creates the following graph:
When I facet the graph to separate it by displ, the x axis do not work anymore although the stats are working.
p3 <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2,2.5)) %>%
ggplot(aes(x=class, y=hwy, col=class))+
geom_violin()+
geom_point()+
facet_grid(cols = vars(displ))+
stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # Add statistical comparisons
theme_bw()+
scale_x_discrete(labels = function(x) {
num_cars <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2,2.5)) %>%
filter(class %in% x) %>%
group_by(class) %>%
summarise(total_cars = n())%>%
mutate(label = total_cars)
num_cars$label
})
p3
Makes this:
When I wanted the tick labels to be 2, 3, 2, 2.
Hopefully someone has an easy answer for this (I am sure it is easy).
Cheers
答案1
得分: 3
一种选项是为每个面板单独设置标签,同时在使用 `stat_compare_means` 时起作用的方法是使用 `ggh4x::facetted_pos_scales`,它允许在每个面板上单独设置比例尺(包括 `labels`):
``` r
library(ggplot2)
library(dplyr, warn=FALSE)
library(ggpubr)
library(ggh4x)
x_labels <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2, 2.5)) %>%
count(class, displ)
x_labels <- split(x_labels, x_labels$displ) %>%
lapply(\(x) x |> select(class, n) |> tibble::deframe())
mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2, 2.5)) %>%
ggplot(aes(x = class, y = hwy, col = class)) +
geom_violin() +
geom_point() +
facet_grid(cols = vars(displ), scales = "free_x") +
stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # 添加统计比较
theme_bw() +
ggh4x::facetted_pos_scales(
x = list(
displ == 2 ~ scale_x_discrete(labels = x_labels$`2`),
displ == 2.5 ~ scale_x_discrete(labels = x_labels$`2.5`)
)
)
<details>
<summary>英文:</summary>
One option to set the labels individually for each panel which at the same time works when using `stat_compare_means` would be to use `ggh4x::facetted_pos_scales` which allows to set the scales (including the `labels`) individually per panel:
``` r
library(ggplot2)
library(dplyr, warn=FALSE)
library(ggpubr)
library(ggh4x)
x_labels <- mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2, 2.5)) |>
count(class, displ)
x_labels <- split(x_labels, x_labels$displ) |>
lapply(\(x) x |> select(class, n) |> tibble::deframe())
mpg %>%
filter(class == c("compact", "subcompact")) %>%
filter(displ == c(2, 2.5)) %>%
ggplot(aes(x = class, y = hwy, col = class)) +
geom_violin() +
geom_point() +
facet_grid(cols = vars(displ), scales = "free_x") +
stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # Add statistical comparisons
theme_bw() +
ggh4x::facetted_pos_scales(
x = list(
displ == 2 ~ scale_x_discrete(labels = x_labels$`2`),
displ == 2.5 ~ scale_x_discrete(labels = x_labels$`2.5`)
)
)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论