0

I'm very new to R/ggplot so thanks in advance for your patience. Here's what I have at the moment:

ggplot(data = figure_data_3A,
       mapping = aes(x = `Gene`,
                     y = `Percent growth`)
       )+

geom_col()+
theme_classic()+
ylab("% Growth of double relative to single mutants")+
xlab(NULL)+
theme(axis.text.x = element_text(angle = 90))

my current graph

Here's my code, and I want to organise the elements on the x-axis by another qualitative factor in my dataframe called Function/Process, so that I can label them together in groups, ultimately to look like this.

here's a snapshot of my data

Peter
  • 11,500
  • 5
  • 21
  • 31
Isaac
  • 3
  • 1
  • 1
    How to make greate, reproducible questions: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – MrGumble Mar 29 '22 at 06:09
  • you're looking for facetted plots https://ggplot2.tidyverse.org/reference/facet_grid.html. In your case, add a layer to your ggplot like `+facet_grid(~\`Function/Process\`)` – tjebo Mar 29 '22 at 12:21

1 Answers1

0

Without a reproducible example (we cannot see your data), we have to guess.

But generally, when plotting a character (text) variable in ggplot2, it gets converted to a factor, and the order of that factor is simply the sorted elements.

Instead, you can preprocess your data, convert Gene to a factor and specify the order.

I think something akin to this would do:

# order your data jf. Function/Process
figure_data3A <- figure_data3A[order(figure_data3A$`Function/Process`),]
# make a factor, 
figure_data_3A$Gene <- factor(figure_data_3A$Gene, levels=figure_data_3A$Gene)

In the last line, I am assuming that each gene only appears once in your data frame.

MrGumble
  • 5,631
  • 1
  • 18
  • 33