有没有办法从 xts 对象中用 ggplot2 绘制 2 个或更多系列?
Is there a way to plot 2 or more series with ggplot2 from an xts object?
基本就是这里描述的问题Plotting an xts object using ggplot2
但是我不能把它改编成两个系列,代码如下:
dates <- c("2014-10-01", "2014-11-01", "2014-12-01", "2015-01-01", "2015-02-01")
values1 <- as.numeric(c(1,2,3,4,5))
values2 <- as.numeric(c(10,9,8,7,6))
new_df <- data_frame(dates, values1, values2)
new_df$dates <- as.Date(dates)
new_df <- as.xts(new_df[, -1], order.by = new_df$dates)
现在我使用 ggplot:
ggplot(new_df, aes(x = Index, y = c(values1, values2)))
+ geom_point()
但我收到以下错误:
Error: Aesthetics must be either length 1 or the same as the data (5):
y Run rlang::last_error()
to see where the error occurred.
这个对象的两个系列可以在同一个地块上吗?
选项 1:将每个系列指定为一个图层:
ggplot(new_df, aes(x = Index)) +
geom_point(aes(y = values1, color = "values1")) +
geom_point(aes(y = values2, color = "values2"))
选项 2:转换为更长的 tibble 形状,以系列名称作为列:
library(tidyverse)
new_df %>%
zoo::fortify.zoo() %>%
as_tibble() %>%
pivot_longer(-Index, names_to = "series", values_to = "values") %>%
ggplot(aes(x = Index, y = values, color = series)) +
geom_point()
关于 new_df 的创建,我们修改了最后注释中的计算,给出了相同的值,但代码更少:
- new_df 是一个 xts 对象,而不是 data.frame,所以让我们使用 x 作为更具描述性的名称
- 创建数据框然后将其转换为 xts 没有实际意义——只需直接创建一个 xts 对象
- 我们不需要 as.numeric。 c(...) 的两个实例都已经是数字。
- ggplot2 命令采用数据帧,而不是 xts 对象。对于 xts 对象,请改用自动绘图。
请注意,注释中的 x
与问题中的 new_df
内容相同。我们刚刚使用了不同的名称。
现在使用autoplot
。如果需要行,请省略 the geom="point"
参数;如果需要单独的面板,请省略 facet=NULL
参数。有关更多示例,请参阅 ?autoplot.zoo
。
library(ggplot2)
library(xts)
autoplot(x, geom = "point", facet = NULL) + ggtitle("My Plot")
备注
上面使用的输入。
library(xts)
dates <- c("2014-10-01", "2014-11-01", "2014-12-01", "2015-01-01", "2015-02-01")
values1 <- c(1,2,3,4,5)
values2 <- c(10,9,8,7,6)
x <- xts(cbind(values1, values2), as.Date(dates))
基本就是这里描述的问题Plotting an xts object using ggplot2
但是我不能把它改编成两个系列,代码如下:
dates <- c("2014-10-01", "2014-11-01", "2014-12-01", "2015-01-01", "2015-02-01")
values1 <- as.numeric(c(1,2,3,4,5))
values2 <- as.numeric(c(10,9,8,7,6))
new_df <- data_frame(dates, values1, values2)
new_df$dates <- as.Date(dates)
new_df <- as.xts(new_df[, -1], order.by = new_df$dates)
现在我使用 ggplot:
ggplot(new_df, aes(x = Index, y = c(values1, values2)))
+ geom_point()
但我收到以下错误:
Error: Aesthetics must be either length 1 or the same as the data (5): y Run
rlang::last_error()
to see where the error occurred.
这个对象的两个系列可以在同一个地块上吗?
选项 1:将每个系列指定为一个图层:
ggplot(new_df, aes(x = Index)) +
geom_point(aes(y = values1, color = "values1")) +
geom_point(aes(y = values2, color = "values2"))
选项 2:转换为更长的 tibble 形状,以系列名称作为列:
library(tidyverse)
new_df %>%
zoo::fortify.zoo() %>%
as_tibble() %>%
pivot_longer(-Index, names_to = "series", values_to = "values") %>%
ggplot(aes(x = Index, y = values, color = series)) +
geom_point()
关于 new_df 的创建,我们修改了最后注释中的计算,给出了相同的值,但代码更少:
- new_df 是一个 xts 对象,而不是 data.frame,所以让我们使用 x 作为更具描述性的名称
- 创建数据框然后将其转换为 xts 没有实际意义——只需直接创建一个 xts 对象
- 我们不需要 as.numeric。 c(...) 的两个实例都已经是数字。
- ggplot2 命令采用数据帧,而不是 xts 对象。对于 xts 对象,请改用自动绘图。
请注意,注释中的 x
与问题中的 new_df
内容相同。我们刚刚使用了不同的名称。
现在使用autoplot
。如果需要行,请省略 the geom="point"
参数;如果需要单独的面板,请省略 facet=NULL
参数。有关更多示例,请参阅 ?autoplot.zoo
。
library(ggplot2)
library(xts)
autoplot(x, geom = "point", facet = NULL) + ggtitle("My Plot")
备注
上面使用的输入。
library(xts)
dates <- c("2014-10-01", "2014-11-01", "2014-12-01", "2015-01-01", "2015-02-01")
values1 <- c(1,2,3,4,5)
values2 <- c(10,9,8,7,6)
x <- xts(cbind(values1, values2), as.Date(dates))