r - 如何对饼图中显示变量的颜色进行硬编码?

标签 r plotly pie-chart

我们有类似 ggplot 的东西吗? 我们为每个变量定义颜色面板?

scale_manual <- function(...){
ggplot2::manual_scale(
"fill",
values = setnames(c("green","red","blue","yellow","grey"),
c("var1","var2","var3","var4","var5")),
...
)
}

虽然这个问题似乎有答案, How can I change the colors of the slices in pie charts in plotly for r using hexadecimal strings?

但它在这里不起作用。

请考虑以下表示:

library(plotly)

USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
data <- USPersonalExpenditure[,c('Categorie', 'X1960')]

p <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie') %>%
  layout(title = 'United States Personal Expenditures by Categories in 1960',
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

#trial 1
plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie', marker = list(color = rainbow(5))) %>%
layout(title = 'United States Personal Expenditures by Categories in 1960', 
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

# trial 2
plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie', marker = list(color = brewer_pal(5, "Set3"))) %>%
layout(title = 'United States Personal Expenditures by Categories in 1960', 
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

由于有许多图使用相同的数据, 颜色需要一致。

因此,尝试对每个变量进行硬编码。

最佳答案

在为 Shiny 的应用程序和绘图图表制作交互式绘图之前,我通常使用leaflet包中的颜色映射函数:

使用您想要的调色板对color变量进行硬编码。

data$color <- leaflet::colorFactor(
  palette = "Dark2", domain = data$Categorie
  )(data$Categorie)

plot_ly(
  data, labels = ~Categorie,  values = ~X1960, type = 'pie',
  marker = list( colors = ~color)
  ) %>%
  layout(
    title = 'United States Personal Expenditures by Categories in 1960', 
    xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
    yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
    )

the pie chart

您也可以手动对其进行硬编码:

colors_list <- list(
  "Food and Tobacco" = "#1B9E77",
  "Household Operation" = "#D95F02",
  "Medical and Health" = "#7570B3",
  "Personal Care" = "#E7298A",
  "Private Education" = "#66A61E"
)

data$color <- dplyr::recode(data$Categorie, !!!colors_list)


plot_ly(
  data, labels = ~Categorie,  values = ~X1960, type = 'pie',
  marker = list( colors = ~color)
) %>%
  layout(
    title = 'United States Personal Expenditures by Categories in 1960', 
    xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
    yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
  )

关于r - 如何对饼图中显示变量的颜色进行硬编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57604060/

相关文章:

python - 在 plotly 中,如何创建链接的 X 轴?

javascript - 图例中的 jqplot 链接

javascript - Highcharts:如何动态移动项目图例位置

使用 facet_wrap 时重新排序 geom_bar

r - 使用 ggplot 将科学计数法转换为小数

r - 如何通过选择 2 列的最大值来执行条件行和

根据列值删除分组依据后的行

r - colorscale不会改变R中plotly 3d散点图中的散点颜色

python - 添加图例并为每个条形添加不同的颜色

android - android中的半饼图