Hi there just started to work with violin plots in R
and I'm pretty fine with the results but, for some reason, despite attempting various alternatives I cannot change the order of the plots on the x-axis. See below for an example:
基本上,我这里有的是八个种群的一系列小提琴曲线图,我展示了它们的变种统计数据;我希望它们按如下顺序排序:AFR、EUR、MENA、SAS、CEA、SIB、OCE和AME,假设它们概括了每一组中发现的变种总数递减的情况.
这是我使用的代码:
library(dplyr)
library(readxl)
library(tibble)
library(ggplot2)
library(hrbrthemes)
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似乎不工作.我 comments 了一行,它将x刻度设置为离散,并覆盖了theme_bw()
选项,我发现here,但我不一定倾向于使用.
任何帮助是非常感谢的,我怀疑问题可能是最初的left_join(df_var) %>%
,如果是这样,我仍然不知道如何解决它.任何帮助是非常感谢,谢谢!
100 output
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"), 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)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -35L))
@stefan得EDIT分
variants_dist <- variants_dist %>%
mutate(population_ID=factor(population_ID, levels=c("AFR", "EUR", "MENA", "SAS", "CEA", "SIB", "OCE", "AME")))
variants_dist %>% arrange(population_ID) -> pop_sort
然后,我把x=pop_count
改成了x=forcats::fct_inorder(pop_count)
这就是你在 comments 中的意思吗?