0

我想在 rPivotTable 中绘制多列。

这是我的数据集。

data_plot = data.frame(month = c(1,2,3,1,2,3), SALES = c(47, 90, 23, 75, 19, 28), promotions = c(3,4,2,5,1,2))
rpivotTable(data_plot)

请参阅此处的数据透视表 1

请参阅此处的数据透视表 2

我想通过使用 rPivotTable 的折线图查看促销活动的销售变化。但我无法同时可视化这两个变量。有什么办法吗?

4

1 回答 1

1

如果 data_plot从宽格式改写为长格式,则两个变量都可以绘制在一张图表中:

library(data.table)
library(rpivotTable)
rpivotTable(melt(data_plot, id.var = "month"))

在此处输入图像描述

对于整形,这里使用的melt()函数。data.table

melt(data_plot, id.var = "month")
   month   variable value
1      1      SALES    47
2      2      SALES    90
3      3      SALES    23
4      1      SALES    75
5      2      SALES    19
6      3      SALES    28
7      1 promotions     3
8      2 promotions     4
9      3 promotions     2
10     1 promotions     5
11     2 promotions     1
12     3 promotions     2
于 2019-04-22T12:56:12.227 回答