We are comparing two datasets (proteome and transcriptome) with matching groups, except for one group missing in the proteome data. As I plot the graphs underneath each other, they would be much easier to compare if the missing group would be represented in the plot.
For this, I need to find a way to plot an "empty" group along the x-axis in a boxplot of ggplot. I could not find a good example for this here or online (though similar questions have been asked) but I didnt get any solution to work for me as my groups are characters and I get the error: Error in factor("a", "b", "c", "d", "e"): unused argument ("e")
example data
I guess there would be a easier way creating this, but it works. Happy for new solution!
library(tidyverse)
crete_exp_df <- function(gene_nr, sample_nr){
df <- replicate(sample_nr, rnorm(gene_nr))
df <- as.data.frame(df)
colnames(df) <- paste("Sample", c(1:ncol(df)))
rownames(df) <- paste("Gene", c(1:nrow(df)))
return(df)
}
df1 <- crete_exp_df(5, 20)
# sample annotation
san <- data.frame(
id = colnames(df1),
group = sample(letters[1:4], 20, replace = TRUE))
df1$gene <- rownames(df1)
# long exp4 box ----
df_long <- df1 %>% pivot_longer(!gene, names_to = "id", values_to = "value")
df_long$group <- as.factor(san$group[match(df_long$id, san$id)])
# plot
df_long %>%
filter(gene == "Gene 1") %>%
ggplot(aes(x = group, y = value)) +
geom_boxplot()