Geom_bar plot with several x-axis variables
0
0
Entering edit mode
6 months ago
sking • 0

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 • 221 views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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")
ADD REPLY
0
Entering edit mode

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 :/

ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 1622 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6