pablete pablete - 1 year ago 78
R Question

How can I plot several series as lines and one of them as area using ggplot2?

I'm trying to accomplish something that I used to do in Excel, I have several timeseries for the same time interval and would like to plot them as lines (easy enough using ggplot geom_line), but one of them should be plotted as an area plot.

Basically something like this:


Plase note that the series S_1 is plotted as area.

I have already tried adding geom_area() with aes values equal to the value of the area series:

ggplot(df.lines, aes(x=Index, y=Value, colour=Series)) + geom_line() + geom_area(aes(x=df.area$Index, y=df.area$S_1))

How could I acomplish something like this using ggplot2?

Answer Source

Difficult to test with no dataset (can you provide one on the example, you can use dput()), but in geom_area, the selection should be made in the data argument.. like this for instance..

ggplot +
geom_area(data = df.area[df.area$Series == "S_1", ], aes(x=Index, y=Value)) 
  geom_line(data = df.lines, aes(x=Index, y=Value, colour=Series))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download