Hi there I'm facing the following problem: I need to custom-order my violin plots for my populations as follow: AFR, EUR, MENA, SAS, CEA, SIB, OCE and AME; however, for some reason, R
doesn't accept my mutate(population_ID = factor(population_ID, levels=c("AFR", "EUR", "MENA", "SAS", "CEA", "SIB", "OCE", "AME"))) %>%
command line which should do exactly so...
Below how the plot appears to be:
and the corresponding code:
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(population_ID = factor(population_ID, levels=c("AFR", "EUR", "MENA", "SAS", "CEA", "SIB", "OCE", "AME"))) %>%
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("")
In principle, it should be a trivial thing to do and I've done it before but in this case is not working. For reference, I also add a link to the post on SO if anyone finds it more helpful.
fracarb8 how can I do so preserving the number of samples for each population for instance. I was trying to act outside the plotting with this:
which is, however, not seen by the
ggplot
graph. Something a person suggested on SO is to use theforcats::fct_inorder(pop_count)
option which I'm not familiar with. Let me know, thanks!fracarb8 thanks for the interest, in the end following the advice given on SO I managed to do so in a clean way. I will still accept the answer because relevant and pointed out in the right direction!