使用mutate(dumr)修改列的值,同时保持组的顺序

ee7vknir  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(70)

我使用这个数据集和代码创建一个嵌套的箱线图,其中组被重新排序,遵循[https://stackoverflow.com/questions/77617469/problem-with-reordering-a-nested-x-axis-with-ggh4x-package:

set1 <- structure(list(Tx = c("Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", 
"Not Exposed", "Not Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", 
"Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Not Exposed", "Not Exposed", 
"Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Exposed", "Exposed", 
"Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", 
"Exposed", "Exposed"), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), levels = c("Species1", "Species2"), class = "factor"), Size = c(88.5, 
83.3, 59.5, 78, 50.3, 57, 78.2, 59, 85, 59.5, 13.1, 50.1, 55, 
60.1, 13.8, 27, 57.1, 53.1, 42, 16, 88.8, 26.2, 62, 108.5, 92.3, 
74.4, 77.3, 96, 88.7, 77.8, 50.7, 61.9, 65.1, 63.5, 64, 88.6, 
53.8, 82.1, 78.8, 75.6)), row.names = c(NA, -40L), class = c("tbl_df", 
"tbl", "data.frame"))

set1 <- set1 %>%
  mutate(w=mean(Size), max = max(Size), .by = c(Tx, Species))

set1$Tx <- factor(set1$Tx, levels = c("Not Exposed", "Exposed"))

set1 <- set1 %>%
  mutate(Tx = paste0(Tx, '\n(n = ', n(), ')'), .by = c(Tx, Species)) 

ggplot(set1, aes(x=interaction(Tx, Species), y=Size)) +
  geom_boxplot() +               
  geom_jitter(width = 0.1, stroke=0.5, size=2) +
  guides(x = "axis_nested") +
  theme_classic()

字符串
下面的代码通常允许我添加每个轴刻度上的观测数(n = ...)。set1 <- set1 %>% mutate(Tx = paste0(Tx, '\n(n = ', n(), ')'), .by = c(Tx, Species))
但是,当在set1$Tx <- factor(set1$Tx, levels = c("Not Exposed", "Exposed"))之后使用时,组不再正确重新排序(在嵌套的x轴中,“Exposed”位于“Not exposed”之前)。

yquaqz18

yquaqz181#

当你创建“Tx(n)”(使用set1 <- set1 %>% mutate(Tx = paste0(Tx, '\n(n = ', n(), ')'), .by = c(Tx, Species)))时,变量类型从factor变为character;如果“Tx(n)”是一个因子,你不会丢失你想要的顺序,例如。

library(tidyverse)
library(ggh4x)

set1 <- structure(list(Tx = c("Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", 
                              "Not Exposed", "Not Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", 
                              "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Not Exposed", "Not Exposed", 
                              "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Exposed", "Exposed", 
                              "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", 
                              "Exposed", "Exposed"), Species = structure(c(1L, 1L, 1L, 1L, 
                                                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                           2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                           2L, 2L, 2L, 2L), levels = c("Species1", "Species2"), class = "factor"), Size = c(88.5, 
                                                                                                                                                            83.3, 59.5, 78, 50.3, 57, 78.2, 59, 85, 59.5, 13.1, 50.1, 55, 
                                                                                                                                                            60.1, 13.8, 27, 57.1, 53.1, 42, 16, 88.8, 26.2, 62, 108.5, 92.3, 
                                                                                                                                                            74.4, 77.3, 96, 88.7, 77.8, 50.7, 61.9, 65.1, 63.5, 64, 88.6, 
                                                                                                                                                            53.8, 82.1, 78.8, 75.6)), row.names = c(NA, -40L), class = c("tbl_df", 
                                                                                                                                                                                                                         "tbl", "data.frame"))

set1 <- set1 %>%
  mutate(w=mean(Size), max = max(Size), .by = c(Tx, Species))

set1$Tx <- factor(set1$Tx, levels = c("Not Exposed", "Exposed"),
                  ordered = TRUE)

set1 <- set1 %>%
  mutate(Tx = factor(paste0(Tx, '\n(n = ', n(), ')')), .by = c(Tx, Species))

ggplot(set1, aes(x=interaction(Tx, Species), y=Size)) +
  geom_boxplot() +               
  geom_jitter(width = 0.1, stroke=0.5, size=2) +
  guides(x = "axis_nested") +
  theme_classic()

字符串
x1c 0d1x的数据
创建于2023-12-10使用reprex v2.0.2
如果要指定顺序,则需要指定“Tx(n)”变量的因子水平,例如:

library(tidyverse)
library(ggh4x)

set1 <- structure(list(Tx = c("Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", 
                              "Not Exposed", "Not Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", 
                              "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Not Exposed", "Not Exposed", 
                              "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Exposed", "Exposed", 
                              "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed", 
                              "Exposed", "Exposed"), Species = structure(c(1L, 1L, 1L, 1L, 
                                                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                           2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                           2L, 2L, 2L, 2L), levels = c("Species1", "Species2"), class = "factor"), Size = c(88.5, 
                                                                                                                                                            83.3, 59.5, 78, 50.3, 57, 78.2, 59, 85, 59.5, 13.1, 50.1, 55, 
                                                                                                                                                            60.1, 13.8, 27, 57.1, 53.1, 42, 16, 88.8, 26.2, 62, 108.5, 92.3, 
                                                                                                                                                            74.4, 77.3, 96, 88.7, 77.8, 50.7, 61.9, 65.1, 63.5, 64, 88.6, 
                                                                                                                                                            53.8, 82.1, 78.8, 75.6)), row.names = c(NA, -40L), class = c("tbl_df", 
                                                                                                                                                                                                                         "tbl", "data.frame"))

set1 <- set1 %>%
  mutate(w=mean(Size), max = max(Size), .by = c(Tx, Species))

set1$Tx <- factor(set1$Tx, levels = c("Not Exposed", "Exposed"),
                  ordered = TRUE)

set1 <- set1 %>%
  mutate(Tx = factor(paste0(Tx, '\n(n = ', n(), ')'),
                     levels = c(paste0("Not Exposed", '\n(n = ', n(), ')'),
                                paste0("Exposed", '\n(n = ', n(), ')'))),
                                .by = c(Tx, Species))

ggplot(set1, aes(x=interaction(Tx, Species), y=Size)) +
  geom_boxplot() +               
  geom_jitter(width = 0.1, stroke=0.5, size=2) +
  guides(x = "axis_nested") +
  theme_classic()



创建于2023-12-10使用reprex v2.0.2

相关问题