r:离散 x 轴上的订单不遵循指定的 relevel

r: Order on discrete x-axis is not following specified relevel

问题x-axis 上的因素没有重新调整

假设我有这个箱线图

使用这些数据生成:

set.seed(1)
y <- data.frame(loglos = log2(runif(1000)),
                corona = as.factor(c(rep("C19", 50), rep("Normal", 50))),
                type = as.factor(c("Vascular", "Hydro", "Trauma", "Tumor", "Infection")))

我的数据集中有多个子类别,我在其中测试 COVID-19 锁定之前和期间类型手术的入院时间平均差异(y$loglos - log2 尺度)。我想申请一个for-loop来完成这个重复的任务

p_perc_ <- c()
p_conf_ <- c()

for(i in unique(y$type)){
  aa <- t.test(y$loglos[y$type == i] ~ relevel(y$corona[y$type == i], ref = "C19"))
  bb <- round(1-2^(aa$estimate[1] - aa$estimate[2]), digits = 3)*(-100)
  p_perc_[i] <- paste0(bb, "%")
  
  p_conf_[i] <- paste0(round(1-2^(aa$conf.int[1]), digits = 3) * -100, 
                      "; ", 
                      round(1-2^(aa$conf.int[2]), digits = 3) * -100)
  
}

目前,x-axis-标签很好地打印了手术名称 (type)、百分比变化和 95% 置信区间。

但是,我想重新排序x-axis,但是使用这些尝试都没有成功。顺序应为:“水肿”、“血管”、“外伤”、“感染”、“肿瘤”

(1)

ggplot(y,
       aes(x = type, y = loglos, color = corona, fill = corona)) + 
  geom_boxplot() + 
  scale_y_continuous(name = "",
                     breaks = seq(-8, 0, 4),
                     limits = c(-8, 0)) +
  
  scale_x_discrete(name = "",
                   limits = c("Hydro", "Vascular", "Trauma", "Infection", "Tumor"),
                   labels = paste0(unique(y$type), "<br>", "<b>", p_perc_, "</b>", "<br><sub>(", p_conf_, ")</sub>")) +
  
  theme(axis.text.x = ggtext::element_markdown(color = "grey20", size = 12))

(2)

ggplot(y,
       aes(x = type, y = loglos, color = corona, fill = corona)) + 
  geom_boxplot() + 
  scale_y_continuous(name = "",
                     breaks = seq(-8, 0, 4),
                     limits = c(-8, 0)) +
  
  scale_x_discrete(name = "",
                   limits = factor(y$type, c("Hydro", "Vascular", "Trauma", "Infection", "Tumor")),
                   labels = paste0(unique(y$type), "<br>", "<b>", p_perc_, "</b>", "<br><sub>(", p_conf_, ")</sub>")) +
  
  theme(axis.text.x = ggtext::element_markdown(color = "grey20", size = 12))
  

(3)

ggplot(y %>% mutate(type = fct_relevel(type, c("Hydro", "Vascular", "Trauma", "Infection", "Tumor"))),
       aes(x = type, y = loglos, color = corona, fill = corona)) + 
  geom_boxplot() + 
  scale_y_continuous(name = "",
                     breaks = seq(-8, 0, 4),
                     limits = c(-8, 0)) +
  
  scale_x_discrete(name = "",
                   limits = unique(y$type),
                   labels = paste0(unique(y$type), "<br>", "<b>", p_perc_, "</b>", "<br><sub>(", p_conf_, ")</sub>")) +
  
  theme(axis.text.x = ggtext::element_markdown(color = "grey20", size = 12))

(4)

ggplot(y %>% mutate(type = factor(type, levels = c("Hydro", "Vascular", "Trauma", "Infection", "Tumor"))),
       aes(x = type, y = loglos, color = corona, fill = corona)) + 
  geom_boxplot() + 
  scale_y_continuous(name = "",
                     breaks = seq(-8, 0, 4),
                     limits = c(-8, 0)) +
  
  scale_x_discrete(name = "",
                   limits = unique(y$type),
                   labels = paste0(unique(y$type), "<br>", "<b>", p_perc_, "</b>", "<br><sub>(", p_conf_, ")</sub>")) +
  
  theme(axis.text.x = ggtext::element_markdown(color = "grey20", size = 12))

这是你想要的吗,看评论?

set.seed(1)
y <- data.frame(loglos = log2(runif(1000)),
                corona = as.factor(c(rep("C19", 50), rep("Normal", 50))),
                type = as.factor(c("Vascular", "Hydro", "Trauma", "Tumor", "Infection")))
str(y$type)
levels(y$type)
#you can see these are ordered in the incorrect way (alphabetical)
#[1] "Hydro"     "Infection" "Trauma"    "Tumor"     "Vascular" 

#wanted order: "Hydro", "Vascular", "Trauma", "Infection", "Tumor
#reorder levels as suggested in comments
myorder <- c("Hydro", "Vascular", "Trauma", "Infection", "Tumor")
y$type <- factor(y$type, levels = myorder)


p_perc_ <- c()
p_conf_ <- c()

for(i in unique(y$type)){
  aa <- t.test(y$loglos[y$type == i] ~ relevel(y$corona[y$type == i], ref = "C19"))
  bb <- round(1-2^(aa$estimate[1] - aa$estimate[2]), digits = 3)*(-100)
  p_perc_[i] <- paste0(bb, "%")
  
  p_conf_[i] <- paste0(round(1-2^(aa$conf.int[1]), digits = 3) * -100, 
                       "; ", 
                       round(1-2^(aa$conf.int[2]), digits = 3) * -100)
  
}

#need to order these too
p_perc_ <- p_perc_[myorder]
p_conf_ <- p_conf_[myorder]

ggplot(y,
       aes(x = type, y = loglos, color = corona, fill = corona)) + 
  geom_boxplot() + 
  scale_y_continuous(name = "",
                     breaks = seq(-8, 0, 4),
                     limits = c(-8, 0)) +
  
  scale_x_discrete(name = "",
                   limits = c("Hydro", "Vascular", "Trauma", "Infection", "Tumor"),
                   #instead of unique, use levels
                   labels = paste0(levels(y$type), "<br>", "<b>", p_perc_, "</b>", "<br><sub>(", p_conf_, ")</sub>")) +
  
  theme(axis.text.x = ggtext::element_markdown(color = "grey20", size = 12))