更改 ggplot2 中小提琴图的顺序

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

您好,刚刚开始在

R
中处理小提琴图,我对结果非常满意,但是,出于某种原因,尽管尝试了各种替代方案,但我无法更改 x 轴上图的顺序。 参见下面示例:

本质上,我这里有八个人群的一系列小提琴图,我在其中显示了他们的变体统计数据;我希望它们按如下顺序排列:AFR、EUR、MENA、SAS、CEA、SIB、OCE 和 AME 据称概括了每组中发现的变体总数递减。

这是我正在使用的代码:

library(grid)
library(ragg)
library(Cairo)
library(ggh4x)
library(readr)
library(dplyr)
library(readxl)
library(tibble)
library(scales)
library(ggpubr)
library(gtable)
library(ggplot2)
library(hrbrthemes)
library(reticulate)
library(colorspace)
library(introdataviz)

variants_dist <- read_excel("path/to/file.xlsm", 10)
df_var = variants_dist %>% group_by(population_ID) %>% summarise(num=n())

### PLOT THE DATA
variants_dist %>%
  left_join(df_var) %>%
  mutate(pop_count = paste0(population_ID, "\n", "n=", num)) %>%
  ggplot(aes(x=pop_count, y=snps, fill=population_ID)) +
  geom_violin(position="dodge", trim=FALSE) +
  geom_boxplot(width=0.07, color="black", alpha=0.6) +
  scale_fill_manual(values=c(EUR="dodgerblue2", MENA="mediumvioletred", SIB="darkkhaki", CEA="firebrick2", AFR="olivedrab2", OCE="powderblue", SAS="darksalmon", AME="plum2")) +
  #scale_x_discrete(limits = c("AFR", "EUR", "MENA", "SAS", "CEA", "SIB", "OCE", "AME")) +
  theme_bw() +
  theme(
    legend.position="none",
  ) +
  xlab("")

我已经按照建议的教程之一获得了这个结果,但不幸的是,像更改顺序这样基本的事情(我通常用

factor
指定levels所需的顺序)似乎不起作用。 .我评论了一行,该行将 x 比例设置为离散并覆盖我在
here
找到的 theme_bw() 选项,但我不一定愿意使用。

非常感谢任何帮助,我怀疑问题可能是最初的

left_join(df_var) %>%
,如果是这样,我仍然不知道如何解决它。非常感谢任何帮助,谢谢!

dput()
输出

structure(list(samples = c("abh100 - number of:", "abh107 - number of:", 
"ALB212 - number of:", "Ale14 - number of:", "Ale20 - number of:", 
"Ale22 - number of:", "Ale32 - number of:", "altai363p - number of:", 
"armenia293 - number of:", "Armenian222 - number of:", "AV-21 - number of:", 
"Ayodo_430C - number of:", "Ayodo_502C - number of:", "Ayodo_81S - number of:", 
"B11 - number of:", "B17 - number of:", "Bishkek28439 - number of:", 
"Bishkek28440 - number of:", "Bu16 - number of:", "Bu5 - number of:", 
"BulgarianB4 - number of:", "BulgarianC1 - number of:", "ch113 - number of:", "CHI-007 - number of:", "CHI-034 - number of:", "DNK05 - number of:", "DNK07 - number of:", "DNK11 - number of:", "Dus16 - number of:", "Dus22 - number of:", "Esk29 - number of:", "Est375 - number of:", "Est400 - number of:", "HG00126 - number of:", "HG00128 - number of:"), population_ID = c("MENA", "MENA", "EUR", "SIB", "SIB", "SIB", "SIB", "SIB", "EUR", "EUR", "EUR", "AFR", "AFR", "AFR", "SAS", "SAS", "SIB", "SIB", "CEA", "CEA", "EUR", "EUR", "EUR", "CEA", "CEA", "AFR", "AFR", "AFR", "OCE", "OCE", "SIB", "EUR", "EUR", "EUR", "EUR", "EUR"), snps = c(4847876, 4820146, 4875942, 4848405, 4846958, 4893150, 
4886498, 4778500, 4868602, 4861225, 5513106, 5726596, 5766508, 
5372587, 4974419, 4894272, 4870208, 4913870, 4923787, 4925207, 
4840414, 4798908, 4891562, 4953420, 4881495, 5605004, 5703805, 
5643221, 4831148, 4829405, 4688483, 4783761, 4778239, 4774887, 
4811481, 4763063, 4811481)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-36L))
r ggplot2 position axis violin-plot
1个回答
0
投票

使用

mutate(pop_count = factor(population_ID, levels = c("AFR", "EUR", "MENA", "SAS", "CEA", "SIB", "OCE", "AME")))

df_var = variants_dist %>% group_by(population_ID) %>% summarise(num=n())

### PLOT THE DATA
variants_dist %>%
  left_join(df_var) %>%
  mutate(pop_count = paste0(population_ID, "\n", "n=", num)) %>%
  mutate(pop_count = factor(population_ID, levels = c("AFR", "EUR", "MENA", "SAS", "CEA", "SIB", "OCE", "AME"))) %>%
  ggplot(aes(x=pop_count, y=snps, fill=population_ID)) +
  geom_violin(position="dodge", trim=FALSE) +
  geom_boxplot(width=0.07, color="black", alpha=0.6) +
  scale_fill_manual(values=c(EUR="dodgerblue2", MENA="mediumvioletred", SIB="darkkhaki", CEA="firebrick2", AFR="olivedrab2", OCE="powderblue", SAS="darksalmon", AME="plum2")) +
  theme_bw() +
  theme(
    legend.position="none",
  ) +
  xlab("")

创建于 2024-03-19,使用 reprex v2.1.0

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