1

I am trying to plot multiple gene expressions over time in the same graph to demonstrate a similar profile and then add a line to illustrate the mean of total for each timepoint (like the figure 4b in recent Nature comm article https://www.nature.com/articles/s41467-017-02546-5/figures/4). My data has been normalised to be around 0 so they are all on the same scale.

df2 sample:

variable    value   gene
1   5   -0.610384193    1
2   5   -6.25967087 2
3   5   -3.773389731    3
50  6   -0.358879035    1
51  6   -6.066341017    2
52  6   -4.202998579    3
99  7   -0.103885903    1
100 7   -6.648844687    2
101 7   -5.041554127    3

I plot the expression levels with ggplot2:

plotC <- ggplot(df2, aes(x=variable, y=value, group=factor(gene), colour=gene)) + geom_line(size=0.5, aes(color=gene), alpha=0.4)

But adding the mean line in red to this plot is proving difficult. I calculated the means and put them in another dataframe:

means
       value variable gene
1 -1.5037354        5   50
2 -0.8783492        6   50
3 -0.7769085        7   50

Then tried adding them as another layer:

plotC + geom_line(data=means, aes(x=variable, y=value, color="red", group=factor(gene)), size=0.75)

But I get an error Error: Discrete value supplied to continuous scale

Do you have any suggestions as to how I can plot this mean on the same graph in another color?

Thank you, Anna

edit: the answer by RG20 is helpful, thanks for pointing out I had the color in the wrong place. However it plots the line outside the rest of the graph... I really don't understand what's wrong with my graph... enter image description here

2
  • 1
    You need to take color = "red" outside of the aes call. Commented Jan 9, 2018 at 15:46
  • Thank you, that seems to solve my error problem, however the line is drawn outside the rest of the graph area. I can't see why it would do this... Commented Jan 9, 2018 at 16:24

1 Answer 1

1
plotC + geom_line(data=means, aes(x=variable, y=value, group=factor(gene)), color='red',size=0.75)
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much, that draws the line, but it puts it outside the other graph areas... it's weird, have a look at the linked photo I put in my original post
@Stav Sounds like your variable column is a factor in your original data and a numeric in your means data. If you convert df2$variable = as.numeric(as.character(df2$variable)) this should work just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.