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
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
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.