使用 long data.frame 或 ggplotly 的 R 交互式堆积面积图

R interactive stacked area chart using long data.frame or ggplotly

我正在根据下面的 plotly 示例代码(找到 here)重新创建交互式绘图,但我想知道是否可以使用长 data.frame 格式来避免添加图例中每个变量的单独 add_trace 函数。类似于 ggplot2 个美学层。

任何交互式绘图解决方案都可以使用(highcharter、plotly 等)。

我还从下面的 ggplotly 创建了一个交互式堆积面积图,但是交互式功能不一样。具体来说,当层在图例处切换 on/off 时,它们不会自行缩放,因此它们沿 x 轴是平坦的。它们按原样出现。例如,如果 colB 是孤立的,它会漂浮在图的中间。

上面的 plotly 示例确实重置了图层,用户可以使用平面 x 轴参考直观地检查各个图层的配置文件。

感谢您的帮助。

library(plotly)

data <- t(USPersonalExpenditure)
data <- data.frame("year"=rownames(data), data)

p <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'none', stackgroup = 'one', fillcolor = '#F5FF8D') %>%
  add_trace(y = ~Household.Operation, name = 'Household Operation', fillcolor = '#50CB86') %>%
  add_trace(y = ~Medical.and.Health, name = 'Medical and Health', fillcolor = '#4C74C9') %>%
  add_trace(y = ~Personal.Care, name = 'Personal Care', fillcolor = '#700961') %>%
  add_trace(y = ~Private.Education, name = 'Private Education', fillcolor = '#312F44') %>%
  layout(title = 'United States Personal Expenditures by Categories',
         xaxis = list(title = "",
                      showgrid = FALSE),
         yaxis = list(title = "Expenditures (in billions of dollars)",
                      showgrid = FALSE))

p

#

library(data.table)
library(magrittr)
library(ggplot2)
library(plotly)
library(lubridate)

dt <- data.table(colA = seq(from = ymd_hms("2020-01-01 00:00:00"),
                            to = ymd_hms("2020-01-01 00:00:00") + days(99),
                            by = "1 day"),
                 colB = runif(100,0,100),
                 colC = runif(100,0,100),
                 colD = runif(100,0,100)) %>% 
  melt(id.vars = "colA")

ggplot <- ggplot(data = dt) +
  geom_area(aes(x = colA,
                y = value,
                fill = variable),
            stat = "identity",
            position = "stack",
            alpha = 0.5) +
  theme(legend.title = element_blank())
ggplot

ggplotly(ggplot)

您可以尝试这样的操作:

library(plotly)

# define your plot
p <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', 
             type = 'scatter', mode = 'none', stackgroup = 'one')

# select the columns you need to plot on the y axis: you remove the year (x axis) 
# and the first one
colstoplot <- setdiff(colnames(data),c("year","Food.and.Tobacco" ))

# now you can loop through the columns in the vector colstoplot
for(i in colstoplot){
  p <- p %>% add_trace(x = data[["year"]], y = data[[i]], name = i)
                       }
# here the plot  
p

@s_t给出的答案绝对有效。但为了完整起见,我将添加另一种方法来完成此操作,它可能更简洁。

您还可以创建一个长数据框并在 plot_ly

中使用 split 参数

来自plot_ly documentation

split: (Discrete) values used to create multiple traces (one trace per value).

这可能会在后台执行与@s_t 选项相同的操作。但它更干净一些。

# create data frame in long format
data.long <- data %>% tidyr::pivot_longer(-year, names_to = "type", values_to = "value")

# create plot_ly using split argument to separate traces according to type
p <- plot_ly(data.long, x = ~year, y = ~value, type = 'scatter', 
             mode = 'none', stackgroup = 'one', split = ~type)

p