R在ggplot中绘制两个facet_grids中的三个时间序列

huangapple go评论61阅读模式
英文:

R Plotting three timeseries in two facet_grids in ggplot

问题

使用facet_grid()函数绘制三个时间序列在两个网格中可能不太容易,因为facet_grid()通常用于创建多个子图,每个子图对应于一个因子水平。但你可以使用其他方法来实现你的目标,比如使用facet_wrap()函数和一些数据预处理来组合两个股票时间序列到一个网格中。以下是示例代码:

library(ggplot2)
library(dplyr)

# 创建一些假数据
set.seed(123)  # 为了复现目的
stock1 = cumprod(1 + c(0, rnorm(99, 0, 0.05)))
stock2 = cumprod(1 + c(0, rnorm(99, 0, 0.075)))
indicator = sample(1:50, 100, replace = TRUE)
date_seq = seq.Date(as.Date("2023-01-01"), length.out = 100, by = 1)

df = data.frame(date = date_seq, stock1 = stock1, stock2 = stock2, indicator = indicator)

# 将股票数据合并到一个列中
df_long = df %>%
  pivot_longer(cols = c(stock1, stock2), names_to = "stock", values_to = "value")

# 创建图形
ggplot(df_long, aes(x = date, y = value, colour = stock)) +
  geom_line() +
  facet_wrap(~ stock, scales = "free") +
  theme_minimal()

这段代码将stock1stock2合并到一个列中,然后使用facet_wrap()函数创建一个包含两个股票的单个图形网格。希望这有助于你的需求。

英文:

Is it possible to plot three timeseries in only two grids using ggplot and facet_grid()?

# Create some fake data
stock1 = cumprod(1+c(0, rnorm(99, 0, .05)))
stock2 = cumprod(1+c(0, rnorm(99, 0, .075)))
indicator = sample(1:50, 100, replace = TRUE)
date_seq = seq.Date(as.Date("2023-01-01"), length.out = 100, by = 1)

df = data.frame(date = date_seq, stock1 = stock1, stock2 = stock2, indicator = indicator)

Now I would like to see an upper graph with the two stocks and one lower graph with the indicator using facet_grid().

The only result I get is a three-grid plot

grid_df = pivot_longer(df, c(stock1, stock2, indicator), names_to = "underlying", values_to = "values")

ggplot(grid_df, aes(x = date, y = values, colour = underlying)) +
     geom_line() +
     facet_grid(vars(underlying), scales = "free")

I dont know how to group the two stocks to bring them into one grid.

Thanks for help!

答案1

得分: 1

以下是您要翻译的内容:

"你可以在你的较长格式数据中添加一列额外的数据,将股票1和股票2合并为一个名为"stocks"的字符串,并使用ifelse将它们分配给facet_grid,如下所示:

library(ggplot2)
library(dplyr)
library(tidyr)
grid_df = pivot_longer(df, c(stock1, stock2, indicator), names_to = "underlying", values_to = "values") %>%
  mutate(grids = ifelse(underlying == "indicator", "indicator", "stock"))

ggplot(grid_df, aes(x = date, y = values, colour = underlying)) +
  geom_line() +
  facet_grid(vars(grids), scales = "free")

R在ggplot中绘制两个facet_grids中的三个时间序列

创建于2023年02月19日,使用reprex v2.0.2"

英文:

You could add an extra column to your longer format data where you could combine the stocks 1 and 2 to one string called stocks and leave the indicator alone using an ifelse to assign them to the facet_grid like this:

library(ggplot2)
library(dplyr)
library(tidyr)
grid_df = pivot_longer(df, c(stock1, stock2, indicator), names_to = "underlying", values_to = "values") %>%
  mutate(grids = ifelse(underlying == "indicator", "indicator", "stock"))

ggplot(grid_df, aes(x = date, y = values, colour = underlying)) +
  geom_line() +
  facet_grid(vars(grids), scales = "free")

R在ggplot中绘制两个facet_grids中的三个时间序列<!-- -->

<sup>Created on 2023-02-19 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年2月19日 20:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定