使用 ggplot2 在同一图形上将两个变量绘制为线 [英] Plotting two variables as lines using ggplot2 on the same graph
本文介绍了使用 ggplot2 在同一图形上将两个变量绘制为线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个非常新的问题,但说我有这样的数据:
A very newbish question, but say I have data like this:
test_data <-
data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
date = seq(as.Date("2002-01-01"), by="1 month", length.out=100)
)
如何在同一个图形上绘制时间序列 var0
和 var1
,在 x 轴上使用 date
,使用 ggplot2
?如果您制作 var0
和 var1
不同的颜色,并且可以包含一个图例,则可以加分!
How can I plot both time series var0
and var1
on the same graph, with date
on the x-axis, using ggplot2
? Bonus points if you make var0
and var1
different colours, and can include a legend!
我确定这很简单,但我找不到任何示例.
I'm sure this is very simple, but I can't find any examples out there.
推荐答案
对于少量变量,您可以自己手动构建绘图:
For a small number of variables, you can build the plot manually yourself:
ggplot(test_data, aes(date)) +
geom_line(aes(y = var0, colour = "var0")) +
geom_line(aes(y = var1, colour = "var1"))
这篇关于使用 ggplot2 在同一图形上将两个变量绘制为线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文