Search code examples
rplotly

Donut plots with same colors for same labels


I am using Plotly to plot Donut Plot. Below you can see my data

df1<-structure(list(manuf = c("AMC", "Cadillac", "Camaro", "Chrysler", 
                              "Datsun", "Dodge", "Duster", "Ferrari", "Fiat", "Ford", "Honda", 
                              "Hornet", "Lincoln", "Lotus", "Maserati", "Mazda", "Merc", "Pontiac", 
                              "Porsche", "Toyota", "Valiant", "Volvo"), count = c(1L, 1L, 1L, 
                                                                                  1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 7L, 1L, 1L, 
                                                                                  2L, 1L, 1L)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", 
                                                                                                                                   "data.frame"))

fig <- df1 %>% plot_ly(labels = ~manuf, values = ~count)
fig <- fig %>% add_pie(hole = 0.6)
fig <- fig %>% layout(title = "Donut charts using Plotly",  showlegend = T,
                      xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                      yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

fig

enter image description here

The above code produces Donut Plot, which you can see below. In this plot, Merc has the largest share of 21 % and is the blue color.

Now I want to plot the same plot but with small changes in data. Now instead of Merc in the first place is AMC with 44.6 %. Below you can see the data and code

    df2<-structure(list(manuf = c("AMC", "Cadillac", "Camaro", "Chrysler", 
                                  "Datsun", "Dodge", "Duster", "Ferrari", "Fiat", "Ford", "Honda", 
                                  "Hornet", "Lincoln", "Lotus", "Maserati", "Mazda", "Merc", "Pontiac", 
                                  "Porsche", "Toyota", "Valiant", "Volvo"), count = c(25L, 1L, 1L, 
                                                                                      1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 7L, 1L, 1L, 
                                                                                      2L, 1L, 1L)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", 
                                                                                                                                       "data.frame"))

fig <- df2 %>% plot_ly(labels = ~manuf, values = ~count)
fig <- fig %>% add_pie(hole = 0.6)
fig <- fig %>% layout(title = "Donut charts using Plotly",  showlegend = T,
                      xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                      yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

fig

enter image description here

Now in this Donut plot, colors are different compared to the first Donut Plot. Namely Merc in the first plot is blue in color while in the second plot is orange.

So can anybody help me how to produce Donut plots with the same colors for the same names.


Solution

  • I used the library shades, but any color palette creator would work. Since every row of the data is equivalent to a pie element, you need the same number of colors as you have rows (22). You can simply add the colors to the data frame.

    library(plotly)
    library(shades)
    
    # since the data frame has all unique manuf
    df1$colr <- gradient("viridis", steps = 22)
    

    By the way, you can check your colors before designation:

    swatch(gradient("viridis", steps = 22))
    

    enter image description here

    While you have plot_ly() and add_pie() separated, you don't need to. Additionally, you can update the figure all at once (instead of updating continuously). Lastly, the following arguments showlegend = T, xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE) are all default settings. You don't need to call any of these.

    df1 %>% plot_ly(labels = ~manuf, values = ~count, type = "pie", 
                    hole = .6, marker = list(colors = ~colr)) %>% 
      layout(title = "Donut charts using Plotly")
    

    enter image description here

    And your other data and plot...

    df2$colr <- gradient("viridis", steps = 22)
    
    plot_ly(data = df2, labels = ~manuf, values = ~count, type = "pie",
            marker = list(colors = ~colr), hole = .6) %>% 
      layout(title = "Donut charts using Plotly")
    

    enter image description here