是否有办法在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)