gpt4 book ai didi

r - 相同标签的相同颜色的 donut chart

转载 作者:行者123 更新时间:2023-12-05 04:21:11 30 4
gpt4 key购买 nike

我正在使用 Plotly 来绘制 Donut Plot。下面你可以看到我的数据

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

上面的代码生成了 donut chart ,您可以在下面看到它。在此图中,Merc 的份额最大,为 21%,是蓝色。

现在我想绘制相同的图,但数据略有变化。现在取代 Merc 的是 AMC,占 44.6%。下面可以看到数据和代码

    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

现在在这个 donut chart 中,颜色与第一个 donut chart 不同。即第一个图中的 Merc 是蓝色,而第二个图中是橙色。

那么谁能帮助我如何为相同的名称生成具有相同颜色的 donut chart 。

最佳答案

我使用了库 shades,但任何调色板创建器都可以使用。由于数据的每一行都相当于一个饼图元素,因此您需要与行数 (22) 相同的颜色数。您可以简单地将颜色添加到数据框。

library(plotly)
library(shades)

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

顺便说一句,您可以在指定之前检查您的颜色:

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

enter image description here

虽然您将 plot_ly()add_pie() 分开,但您不需要这样做。此外,您可以一次更新所有图形(而不是连续更新)。最后,以下参数 showlegend = T, xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE) 是所有默认设置。您不需要调用其中任何一个。

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

还有你的其他数据和图...

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

关于r - 相同标签的相同颜色的 donut chart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74301527/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com