custom colors in R Plotly

前端 未结 4 2059
没有蜡笔的小新
没有蜡笔的小新 2021-01-12 00:57

I\'m currently a beginner in Plotly and have a problem I can\'t seem to solve. I have my plotly stacked bar chart but I don\'t know how to color each individual category. I

4条回答
  •  猫巷女王i
    2021-01-12 01:55

    Here are a few modifications (~,and ~factor())to the plot_ly statement to get the output to work with newer versions of plotly.

    require(dplyr)
    require(plotly)
    
    set.seed(42)
    
    df <- data.frame(x = rep(LETTERS[1:5], 3), 
                     y = rexp(15, rate = 0.5),
                     z = c(rep("Adam", 5), rep("Arthur", 5), rep("Ford", 5)))
    df <- arrange(df, desc(z))
    
    plot_ly(df, 
            x = ~x, 
            y = ~y, 
            color = ~factor(z), 
            colors = c("grey50", "blue", "red"), 
            type = "bar") %>% 
      layout(barmode = "stack")
    

    cheers

提交回复
热议问题