使用GGPLOT2在同一图上使用GGPLOT2绘制两个变量

发布于 2025-02-10 21:05:01 字数 449 浏览 4 评论 0原文

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

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 var1 ,date在X轴上,使用ggplot2?如果您制作var0var1不同的颜色,则可以包括传奇!

我确定这很简单,但是我找不到任何示例。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゞ记忆︶ㄣ 2025-02-17 21:05:01

对于少数变量,您可以自己手动构建情节:

ggplot(test_data, aes(date)) + 
  geom_line(aes(y = var0, colour = "var0")) + 
  geom_line(aes(y = var1, colour = "var1"))

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"))
彼岸花ソ最美的依靠 2025-02-17 21:05:01

一般方法是将数据转换为长格式(使用Melt()来自package reshapereshape2)或gather()/pivot_longer()来自tidyr软件包:

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

## convert to long format with tidyr::pivot_longer
test_data_long_tidyr <- pivot_longer(test_data, cols = starts_with("var"))

ggplot(data=test_data_long_tidyr,
       aes(x=date, y=value, colour=name)) +
  geom_line() ## output not shown, it's equivalent to the below graph (with a tiny difference in the legend title)

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

ggplot(data=test_data_long,
       aes(x=date, y=value, colour=variable)) +
  geom_line()

“”

另请参阅这个问题

The general approach is to convert the data to long format (using melt() from package reshape or reshape2) or gather()/pivot_longer() from the tidyr package:

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

## convert to long format with tidyr::pivot_longer
test_data_long_tidyr <- pivot_longer(test_data, cols = starts_with("var"))

ggplot(data=test_data_long_tidyr,
       aes(x=date, y=value, colour=name)) +
  geom_line() ## output not shown, it's equivalent to the below graph (with a tiny difference in the legend title)

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

ggplot(data=test_data_long,
       aes(x=date, y=value, colour=variable)) +
  geom_line()

Also see this question on reshaping data from wide to long.

吻安 2025-02-17 21:05:01

对于GGPLOT2,您需要以“高”格式而不是“宽”数据。 “宽”是指每个变量作为不同的列(就像您现在的列一样)。您需要将其转换为“高个子”格式,其中有一个列,该列告诉您变量的名称和另一列,该列告诉您变量的值。从宽到高的过程通常称为“熔化”。您可以使用tidyr ::收集熔化数据框架:

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

”多重系列ggplot2“

为了清楚data ggplot正在消耗通过收集将其输送后,看起来像这样:

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

You need the data to be in "tall" format instead of "wide" for ggplot2. "wide" means having an observation per row with each variable as a different column (like you have now). You need to convert it to a "tall" format where you have a column that tells you the name of the variable and another column that tells you the value of the variable. The process of passing from wide to tall is usually called "melting". You can use tidyr::gather to melt your data frame:

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

multiple series ggplot2

Just to be clear the data that ggplot is consuming after piping it via gather looks like this:

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
总攻大人 2025-02-17 21:05:01

continue

I am also new to R but trying to understand how ggplot works I think I get another way to do it. I just share probably not as a complete perfect solution but to add some different points of view.

I know ggplot is made to work with dataframes better but maybe it can be also sometimes useful to know that you can directly plot two vectors without using a dataframe.

Loading data. Original date vector length is 100 while var0 and var1 have length 50 so I only plot the available data (first 50 dates).

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)    

Plotting

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

enter image description here

However I was not able to add a correct legend using this format. Does anyone know how?

假装不在乎 2025-02-17 21:05:01

使用您的数据:

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

我创建一个堆叠版本,这是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)))

在这种情况下,生产堆叠非常容易,因为我们只需要做一个几个操作,但是reshape()reshapereshape2,如果您有一个更复杂的真实数据集可以操纵。

一旦数据以此堆叠的形式,它只需要一个简单的ggplot()调用来制作您想要的所有附加功能的绘图(一个原因是lattice lattice <的高级绘图包的原因之一/code>和ggplot2非常有用):

require(ggplot2)
p <- ggplot(stacked, aes(Dates, value, colour = variable))
p + geom_line()

我将其留给您以整理轴标签,传奇标题等

Using your data:

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

I create a stacked version which is what ggplot() would like to work with:

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

In this case producing stacked was quite easy as we only had to do a couple of manipulations, but reshape() and the reshape and reshape2 might be useful if you have a more complex real data set to manipulate.

Once the data are in this stacked form, it only requires a simple ggplot() call to produce the plot you wanted with all the extras (one reason why higher-level plotting packages like lattice and ggplot2 are so useful):

require(ggplot2)
p <- ggplot(stacked, aes(Dates, value, colour = variable))
p + geom_line()

I'll leave it to you to tidy up the axis labels, legend title etc.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文