简体   繁体   English

如何使用ggplot2将几个系列绘制为线,将其中一个绘制为面积?

[英]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. 我正在尝试完成以前在Excel中所做的事情,在相同的时间间隔内有多个时间序列,并希望将它们绘制为线(使用ggplot geom_line足够容易),但是其中之一应绘制为面积情节。

Basically something like this: 基本上是这样的:

期望

Plase note that the series S_1 is plotted as area. 请注意,系列S_1被绘制为面积。

I have already tried adding geom_area() with aes values equal to the value of the area series: 我已经尝试添加aes值等于区域序列值的geom_area():

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? 我怎样才能使用ggplot2完成这样的事情?

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.. 没有数据集很难测试(可以在示例中提供一个数据集,可以使用dput() ),但是在geom_area ,应该在data参数中进行选择。例如,像这样。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM