gpt4 book ai didi

r - 在 qqplot 中的多个图上绘制多个子图

转载 作者:行者123 更新时间:2023-12-02 02:43:55 24 4
gpt4 key购买 nike

我有一个数据框,其中包含不同国家和不同变量的时间序列数据。假设有两个国家(英国、美国)和两个变量(GMS、PP)——对于每个国家,我想为每个变量绘制两个时间序列,一个与另一个。

这意味着我想要 2 个带有 2 个子图的地 block ,即英国将有两个地 block ,其中我有 GMS 和 PP 的时间序列(美国相同)。

我还想在图中添加一个图例。

       month marketplace value_fcst_1 value_fcst_2 variable
1 2019-05-26 US 4202393 4198816 GMS
2 2019-06-02 US 30504725 31525980 GMS
3 2019-06-09 US 30454694 30602385 GMS
4 2019-06-16 US 30249561 30363117 ALC
5 2019-06-23 US 30884821 31682497 ALC
6 2019-06-30 US 31424970 31198360 ALC
7 2019-05-26 UK 4202393 4198816 GMS
8 2019-06-02 UK 30504725 31525980 GMS
9 2019-06-09 UK 30454694 30602385 GMS
10 2019-06-16 UK 30249561 30363117 ALC
11 2019-06-23 UK 30884821 31682497 ALC
12 2019-06-30 UK 31424970 31198360 ALC

我设法绘制了所有变量,但不确定如何划分美国和英国的图表以及如何调整每个变量的 y 轴,因为比例不匹配(见图)。
series_plot <- ggplot(data = final_df) +
geom_line(aes(x = month, y = value_fcst_1), colour = 'dodgerblue2', na.rm = TRUE, show.legend = TRUE) +
geom_line(aes(x = month, y = value_fcst_2), colour = 'coral2', na.rm = TRUE, show.legend = TRUE) +
facet_wrap(vars(variable)) +
labs(x = 'Months') +
labs(title = 'Comparisons of two different forecast runs', subtitle = '2019-05-31 vs 2019-06-30 forecast runs')
# labs(name = 'Forecast Runs', fill = 'buu') +
# legend("test1","test2")
print(series_plot)

Output of code below

最佳答案

您可以在 facet_* 中释放一个或两个天平职能。

( 更新 :我认为您最近的评论建议稍微 reshape 数据......滚动到底部以另一种方式查看它。)

使用您的样本数据,保持“x”相同但免费的“y”:

ggplot(data = final_df) +
geom_line(aes(x = month, y = value_fcst_1), colour = 'dodgerblue2', na.rm = TRUE, show.legend = TRUE) +
geom_line(aes(x = month, y = value_fcst_2), colour = 'coral2', na.rm = TRUE, show.legend = TRUE) +
facet_wrap(vars(variable), scales="free_y") +
labs(x = 'Months') +
labs(title = 'Comparisons of two different forecast runs', subtitle = '2019-05-31 vs 2019-06-30 forecast runs')

y axis independent

释放“x”和“y”:

ggplot(data = final_df) +
geom_line(aes(x = month, y = value_fcst_1), colour = 'dodgerblue2', na.rm = TRUE, show.legend = TRUE) +
geom_line(aes(x = month, y = value_fcst_2), colour = 'coral2', na.rm = TRUE, show.legend = TRUE) +
facet_wrap(vars(variable), scales="free") +
labs(x = 'Months') +
labs(title = 'Comparisons of two different forecast runs', subtitle = '2019-05-31 vs 2019-06-30 forecast runs')

enter image description here

更新 :根据运行预测的时间“添加图例”的最佳方法是让 ggplot2为你而做。要做到这一点,你需要它 一个变量,而不是 一个变量。现在,你有 value_fcst_1作为变量, value_fcst_2作为变量。让我们 reshape 数据。我正在使用 dplyrtidyr在这里,虽然有 base 和 data.table方法也是如此。

library(dplyr) # and tidyr is used
final_df %>%
tidyr::gather(k, v, -month, -marketplace, -variable) %>%
slice(1:3, n() - 0:2) # just to show some sampling
# month marketplace variable k v
# 1 2019-05-26 US GMS value_fcst_1 4202393
# 2 2019-06-02 US GMS value_fcst_1 30504725
# 3 2019-06-09 US GMS value_fcst_1 30454694
# 4 2019-06-30 UK ALC value_fcst_2 31198360
# 5 2019-06-23 UK ALC value_fcst_2 31682497
# 6 2019-06-16 UK ALC value_fcst_2 30363117

这将预测运行放入一个变量中(此处名为 k)。从这里开始,很容易做到

final_df %>%
tidyr::gather(k, v, -month, -marketplace, -variable) %>%
ggplot() +
geom_line(aes(x = month, y = v, color = k), na.rm = TRUE, show.legend = TRUE) +
facet_wrap(vars(variable), scales="free") +
labs(x = 'Months') +
labs(title = 'Comparisons of two different forecast runs', subtitle = '2019-05-31 vs 2019-06-30 forecast runs')

legend options for "forecast run"
k确实很难看,但我故意保留它,因为有两个简单的修复方法:
  • 使用 tidyr::gather("Forecast Run", v, ...) , 虽然这需要 `Forecast Run` (反引号!)作为变量名(由于空格);或
  • 添加 scale_color_discrete(name = "Forecast Run") ,它的好处是使用“更简单”的东西,如 k (好吧,也许单字母变量名太简洁了)到处都是,但仍然允许一个好的图例名称。

  • 每个都有其好处/优势。

    关于r - 在 qqplot 中的多个图上绘制多个子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186606/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com