0

I am working with Plotly. My idea is to make plot with different colors, where for each variable I can see different color. Below you can see my data and my code.

    library(dplyr)
    library(plotly)
    
    # Sample data frame (replace this with your actual dataset)
    data <-

 data.frame(
  Column1 = c(1, 2, 3, 4),
  Column2 = c(5, 6, 7, 8),
  Column3 = c(9, 10, 11, 12),
  Column4 = c(13, 14, 15, 16)
)

# Calculate cumulative sums for each column
data <- data %>%
  mutate(
    Cumulative_Column1 = cumsum(Column1),
    Cumulative_Column2 = cumsum(Column2),
    Cumulative_Column3 = cumsum(Column3),
    Cumulative_Column4 = cumsum(Column4))

Now I am trying to make plot of this data with Plotly.

Chart1 <- plot_ly(
  data,
  x = ~Column1,
  y = ~Cumulative_Column1,
  name = 'Cumulative_Column1',
  type = 'scatter',
  mode = 'lines',
  fill = 'tozeroy'
) %>%
  add_trace(x = ~Column1, y = ~Cumulative_Column2 , name = 'Cumulative_Column2') %>%
  add_trace(x = ~Column1, y = ~Cumulative_Column3 , name = 'Cumulative_Column3') %>%
  add_trace(x = ~Column1, y = ~Cumulative_Column4 , name = 'Cumulative_Column4') %>%
  layout(
    xaxis = list(
      title = ' ',
      tickmode = 'linear',
      dtick = 1  # Set the interval to 1 for integer years
    ),
    yaxis = list(title = ''),
    title = "",
    annotations = list(
      x = 0,
      y = -0.09,
      title = " ",
      text = "Source: ",
      showarrow = F,
      xref = 'paper',
      yref = 'paper'
    )
  )

Unfortunately this data give me a chart as chart below with mixed (overlapping) colors, instead to have colors as showed in the legend of the Chart.

enter image description here

Can anybody help me how to solve this problem ?

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.