如何更改 R 中堆积面积图的颜色?

问题描述 投票:0回答:1

这是我当前的绘图,但我无法更改区域的颜色!

df_long <- structure(list(Date = structure(c(19394, 19395, 19396, 19397, 19394, 19395, 19396, 19397, 19394, 19395, 19396, 19397, 19394, 19395, 19396, 19397, 19394, 19395, 19396, 19397, 19394, 19395, 19396, 19397), class = "Date"), Variable = structure(c(6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), levels = c("collateral_bb_amount", "net_collateral_balance", "eligible_collateral_balance", "adjusted_collateral_balance", "total_collateral_balance", "raw_collateral_balance"), class = "factor"),     Value = c(4.4608541901689, 4.64286065101624, 3.69682048354298,     3.10567363910377, 7.86837700684555, 9.50539807975292, 6.7145042992197,     6.34911513398401, 9.4028416336514, 7.84154358459637, 12.250823948998,     12.167556893779, 12.4405719409697, 9.63215040974319, 8.25254024844617,     9.13952640793286, 8.39749736152589, 7.17061420762911, 12.0233266334981,     13.6932893700432, 57.4298578668386, 61.2074330672622, 57.0619843862951,     55.5448385551572), pretty_Variable = structure(c(6L, 6L,     6L, 6L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 2L,     2L, 2L, 2L, 1L, 1L, 1L, 1L), levels = c("Collateral bb amount",     "Net collateral balance", "Eligible collateral balance",     "Adjusted collateral balance", "Total collateral balance",     "Raw collateral balance"), class = "factor")), row.names = c(37L, 38L, 39L, 40L, 77L, 78L, 79L, 80L, 117L, 118L, 119L, 120L, 157L, 158L, 159L, 160L, 197L, 198L, 199L, 200L, 237L, 238L, 239L, 240L), class = "data.frame")

p <- plot_ly(x = as.Date(df_long$Date), y = df_long$Value, color = df_long$pretty_Variable, fill = "tonexty",
             type = "scatter", mode = "stack", stackgroup = "one") %>%
  layout(xaxis = list(title = "Date"),
         yaxis = list(title = "Percentage (%)", ticksuffix = "%"),
         hovermode = "x")
p

r plotly
1个回答
0
投票

我们可以这样做。创建颜色向量并将其分配给颜色参数:

library(plotly)

my_colors <- c("gold", "green", "blue", "steelblue", "red", "purple")

p <- plot_ly(data = df_long, x = ~Date, y = ~Value, color = ~pretty_Variable, 
             colors = my_colors, fill = "tonexty", 
             type = "scatter", mode = "stack", stackgroup = "one") %>%
  layout(xaxis = list(title = "Date"),
         yaxis = list(title = "Percentage (%)", ticksuffix = "%"),
         hovermode = "x")

p

© www.soinside.com 2019 - 2024. All rights reserved.