首页 > 解决方案 > R-plotly中带有堆积面积图的叠加线

问题描述

有没有办法在 Plotly R 中用堆积面积图覆盖一条线?这是示例代码,但它并没有让我得到我想要的。我希望这条线不要堆叠在面积图上,而是独立的。它类似于供需问题。

library(plotly)

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

fig <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', 
                type = 'scatter', mode = 'lines', stackgroup = 'one', fillcolor = '#F5FF8D')

fig <- fig %>% add_trace(y = ~Household.Operation, name = 'Household Operation', 
                          fillcolor = '#50CB86')

fig <- fig %>% add_trace(y = ~Medical.and.Health, name = 'Medical and Health', 
                          fillcolor = '#4C74C9')

fig <- fig %>% add_trace(y = ~Personal.Care, name = 'Personal Care', 
                          fillcolor = '#700961')

fig <- fig %>% add_trace(y = ~Private.Education, name = 'Private Education', 
                          fillcolor = '#312F44')

fig <- fig %>% layout(title = 'United States Personal Expenditures by Categories',
                      xaxis = list(title = "", showgrid = FALSE),
                      yaxis = list(title = "Expenditures (in billions of dollars)", 
                                   showgrid = FALSE))

fig %>% 
  add_lines(x= data$year, y = 200)

标签: rplotlyr-plotly

解决方案


推荐阅读