Having troubles making a boxplot

I'm having some troubles making a boxplot, this is what I tried:

ggplot(complete.tbl, aes(x=reorder(Group, Genes, FUN = median), y=Genes)) + geom_boxplot() + xlab("Grupper") + ylab("Antall gener")

And this is the output...

I've tried this code before with another dataframe, so I guess its something wrong with my data? Anyone know what's wrong here?

Thanks :slight_smile:

Please post a sample of complete.tbl (copy-friendly text version can be generated using dput(head(complete.tbl, n = 10)).

Also, can you please explain the purpose of calling reorder() inside aes()? What transformation are you trying to perform before plotting?

Here is a sample of complete.tbl:

> dput(head(complete.tbl, n = 10))
structure(list(Size = c(167.676, 119.669, 979.046, 412.924, 828.349, 
4340.66, 374.423, 15418.8, 2135.08, 7.66146), Group = c("Protists", 
"Plants", "Plants", "Plants", "Plants", "Plants", "Plants", "Plants", 
"Plants", "Fungi"), Genes = c("38549", "38311", "59906", "37734", 
"31275", NA, "35223", NA, "49339", "3695"), GC = c("64.5", "36.0529", 
"35.1153", "34.047", "35.6991", "44.5703", "43.5769", "46.113", 
"46.9109", "27.8"), Status = c("Scaffold", "Chromosome", "Chromosome", 
"Chromosome", "Chromosome", "Chromosome", "Chromosome", "Chromosome", 
"Chromosome", "Contig"), SizeKb = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"

I used reorder() to sort the boxes in the plot by median.

Genes is coded as a character variable. As a result, ggplot is treating each gene as its own category, resulting in a separate boxplot for each unique gene within each Group. To get a single boxplot for each Group, recode Genes to be numeric (complete.tbl$Genes = as.numeric(complete.tbl$Genes)). You can also see this directly in the plot, where every level of Genes has a tickmark on the y-axis.

1 Like

That solved the problem! Thank you! :))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.