在同一个图上使用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
和使用
ggplot2
?在同一个图上使用 date
如果您制作 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 use build up 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屋!
查看全文