This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Geom_bar plot with several x-axis variables

Hi, I'm trying to create a bar plot whose x-axis has several variables (measuring gene expression in two genotypes with three treatment conditions collected on various days (after disease induction)).

Ideally the graph would be depicted by day on the x-axis (0, 1, 3, 5). Each day would have genotypes in opposing colors and treatments ordered within genotype. I found this post that is similar Example graph but the answer using iris data lacks one variable (genotype).

Here's what the data look like:

# A tibble: 24 x 6

sample genotype treatment   day gene1 gene2
<dbl> <chr>    <chr>     <dbl> <dbl> <dbl>
1      1 WT       Control    0 1     0.933
2      2 WT       Treatment1 0 1.09  6.73
3      3 WT       Treatment2 0 0.759 7.78
4      4 KO       Control    0 0.725 0
5      5 KO       Treatment1 0 0.624 0
6      6 KO       Treatment2 0 0.820 0 
7      7 WT       Control    1 0.863 0.821
8      8 WT       Treatment1 1 0.991 5.45 
9      9 WT       Treatment2 1 0.871 7.21
10     10 KO       Control   1 0.634 0
# ... with 14 more rows

Thanks in advance!

ggplot2 r bar plot x-axis

What have you tried? What does your ggplot code look like right now?

I've tried this but I get stacked bars because of the treatment. I just don't know how to add in another 'layer' to the x-axis.

ggplot(data, aes(x=day, y=gene1, fill=genotype))+geom_bar(stat = "identity", position = "dodge", color ="black")

I also tried to reshape the data as described in this post, but since I have three non-continuous variables it doesn't seem to work :/

I have a feeling you're trying to visualize too many variables in a single static plot. If you need individual non-stacked bars, your x needs to be treatment, not day. Try using that to plot for a single value of day. You can then use facets to add in the day component.

0 answers

No answers yet.

Log in to answer this question.