在同一图表上使用ggplot2将两个变量绘制为线条

fma*_*ark 293 r graph time-series ggplot2 r-faq

这是一个非常新的问题,但我说有这样的数据:

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)
  )
Run Code Online (Sandbox Code Playgroud)

如何在x轴上使用?绘制时间序列var0var1同一图表?奖励积分,如果你做和不同的颜色,并且可以包括一个传奇!dateggplot2var0var1

我确信这很简单,但我找不到任何例子.

had*_*ley 360

对于少量变量,您可以自己手动构建绘图:

ggplot(test_data, aes(date)) + 
  geom_line(aes(y = var0, colour = "var0")) + 
  geom_line(aes(y = var1, colour = "var1"))
Run Code Online (Sandbox Code Playgroud)

  • `+ scale_colour_manual(values = c("black","orange"))` (11认同)
  • 如何为其添加图例? (6认同)
  • 一个很好的例子,但是如何自定义我自己的颜色(例如黑色和橙色)?,因为似乎您使用的是“ colour =”作为变量名。 (2认同)
  • 即使是 hadley 指定的 `colour='var_names'` 也能正常工作。但是@DaveX - 如果想要选择特定的颜色而不是由函数自动选择的颜色会更具体。 (2认同)

rcs*_*rcs 349

一般方法是将数据转换为长格式(使用melt()reshapereshape2)或gather()pivot_longer()包中:

library("reshape2")
library("ggplot2")

test_data_long <- melt(test_data, id="date")  # convert to long format

ggplot(data=test_data_long,
       aes(x=date, y=value, colour=variable)) +
       geom_line()
Run Code Online (Sandbox Code Playgroud)

ggplot2输出

  • 你也可以使用`tidyr`包的`gather()`函数来融合数据:`gather(test_data,variable,value,-date)` (8认同)
  • reshape2 和 tidyr::gather 被取代(请参阅 https://github.com/hadley/reshape 和 `?tidyr::gather`)。Hadley 建议使用“pivot_longer” - 我添加了后者的示例 (2认同)

ece*_*ulm 32

对于ggplot2,您需要数据为"高"格式而不是"宽"."宽"表示每行观察一次,每个变量作为不同的列(就像你现在一样).您需要将其转换为"高"格式,其中有一列告诉您变量的名称,另一列告诉您变量的值.从宽到高的过程通常被称为"融化".您可以tidyr::gather用来融合数据框:

library(ggplot2)
library(tidyr)

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)
  )
test_data %>%
    gather(key,value, var0, var1) %>%
    ggplot(aes(x=date, y=value, colour=key)) +
    geom_line()
Run Code Online (Sandbox Code Playgroud)

多个系列ggplot2

只是为了清楚data,ggplot通过gather看起来像这样消耗它:

date        key     value
2002-01-01  var0    100.00000
2002-02-01  var0    115.16388 
...
2007-11-01  var1    114.86302
2007-12-01  var1    119.30996
Run Code Online (Sandbox Code Playgroud)


Rei*_*son 13

使用您的数据:

test_data <- data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
Dates = seq.Date(as.Date("2002-01-01"), by="1 month", length.out=100))
Run Code Online (Sandbox Code Playgroud)

我创建了一个堆栈版本,这是我ggplot()想要使用的:

stacked <- with(test_data,
                data.frame(value = c(var0, var1),
                           variable = factor(rep(c("Var0","Var1"),
                                                 each = NROW(test_data))),
                           Dates = rep(Dates, 2)))
Run Code Online (Sandbox Code Playgroud)

在这种情况下,生产stacked是很容易的,因为我们只需要做到几个操作的,但reshape()reshapereshape2你有更复杂的真实数据集来操作可能是有用的.

一旦数据在此堆放形式,只需要一个简单的ggplot()通话产生你所有的演员想要的情节(原因之一更高级别的绘图包,比如latticeggplot2是如此有用):

require(ggplot2)
p <- ggplot(stacked, aes(Dates, value, colour = variable))
p + geom_line()
Run Code Online (Sandbox Code Playgroud)

我会留给你整理轴标签,图例标题等.

HTH


Pap*_*gui 6

我对R也很陌生,但是想了解ggplot的工作原理,我想我有另一种方法。我只是分享可能不是一个完整的完美解决方案,而是要添加一些不同的观点。

我知道ggplot可以更好地与数据帧一起使用,但是知道不使用数据帧就可以直接绘制两个向量有时也许对您很有用。

加载数据中。原始日期向量的长度为100,而var0和var1的长度为50,因此我仅绘制可用数据(前50个日期)。

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=50)    
Run Code Online (Sandbox Code Playgroud)

绘图

ggplot() + geom_line(aes(x=date,y=var0),color='red') + 
           geom_line(aes(x=date,y=var1),color='blue') + 
           ylab('Values')+xlab('date')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

但是,我无法使用这种格式添加正确的图例。有人知道吗?

  • 这会添加一个图例 `ggplot() + geom_line(aes(x=date,y=var0, group=1, color = 'red')) + geom_line(aes(x=date,y=var1, group = 2, color) = '蓝色')) + ylab('值')+xlab('日期')` (5认同)