针对特定处理按升序排列箱线图的值(ggplot)

问题描述 投票:0回答:1

我想根据特定处理的升序值来排列我的箱线图。目前,它们按字母顺序显示。

我如何修改我的代码,使它们按升序排列并基于两种待遇之一(例如 VIP)? 这是我在 excel 中的数据集的示例(编辑:重组以方便 R 导入):

Peso <- structure(list(Accession = c("Potato", "Potato", "Potato", "Potato", 
"Potato", "Potato"), Genotype = c("#1", "#2", "#3", "#4", "#5", 
"#6"), Code = c("aa", "ab", "ac", "ad", "ae", "af"), Nome = c("L", 
"L", "L", "L", "L", "L"), Data = c("A", "A", "A", "A", "A", "A"
), Treatment = c("VIP", "VIP", "VIP", "MOCK", "MOCK", "MOCK"), 
    Weight = c(1.9, 1.46, 1.52, 1.29, 1.05, 1.52)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L))

这是我的代码:

Population <- ggplot(data = na.omit(Peso), aes(x = Code, y = Weight, fill = Treatment)) + 
  geom_boxplot( alpha=0.2) + 
  xlab("Genotype") + 
  geom_point()+
  ylim(0, 3.00)

Population + ggtitle("21dpi Dry-Weight") +
  ylab("Weight (g)") +
  xlab("Genotype") +
  theme(plot.title = element_text(family = NULL, face = NULL, color = "black", size = 15)) +
  theme(axis.title.x = element_text(family = NULL, face = NULL, color = "black", 14)) +
  theme(axis.title.y = element_text(family = NULL, face = NULL, color = "black", 14)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1, size = 9)) + 
  scale_fill_manual(values=c("#33FF00", "#000000"))
r ggplot2 boxplot
1个回答
0
投票

除非另有说明,X 轴中的类别顺序按字母顺序排列。 我们可以通过将

Code
设为有序因子来做到这一点,首先基于
Treatment
,其次基于
Weight

测试数据

Peso <- tibble::tribble(~Accession, ~Genotype, ~Code, ~Nome, ~Data, ~Treatment, ~Weight,
                      "Potato", "#1", "aa", "L", "A", "VIP", 1.90,
                      "Potato", "#2", "ab", "L", "A", "VIP", 1.46,
                      "Potato", "#3", "ac", "L", "A", "VIP", 1.52,
                      "Potato", "#4", "ad", "L", "A", "MOCK", 1.29,
                      "Potato", "#5", "ae", "L", "A", "MOCK", 1.05,
                      "Potato", "#6", "af", "L", "A", "MOCK", 1.52)

设置具有有序级别的因子:

Peso$Code <- factor(Peso$Code, levels = unique(Peso$Code[order(Peso$Treatment, Peso$Weight, Peso$Code)]), ordered = TRUE)

然后你的情节:

Population <- ggplot(data = na.omit(Peso), aes(x = Code, y = Weight, fill = Treatment)) + 
  geom_boxplot( alpha=0.2) + 
  xlab("Genotype") + 
  geom_point()+
  ylim(0, 3.00)

Population + ggtitle("21dpi Dry-Weight") +
  ylab("Weight (g)") +
  xlab("Genotype") +
  theme(plot.title = element_text(family = NULL, face = NULL, color = "black", size = 15)) +
  theme(axis.title.x = element_text(family = NULL, face = NULL, color = "black", 14)) +
  theme(axis.title.y = element_text(family = NULL, face = NULL, color = "black", 14)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1, size = 9)) + 
  scale_fill_manual(values=c("#33FF00", "#000000"))

© www.soinside.com 2019 - 2024. All rights reserved.