如何在 plotly 饼图中硬编码显示变量的颜色?
How to hard code the color for the displayed variable in 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")),
...
)
}
虽然这个Q好像回答了,
但它在这里不起作用。
请考虑以下代表:
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))
由于有很多地块使用相同的数据,
颜色要一致
因此,尝试对每个变量进行硬编码。
在为闪亮的应用程序和绘图图表制作交互式图表之前,我通常使用 leaflet
包中的 Color mapping
函数:
使用您想要的调色板对 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)
)
您也可以手动对其进行硬编码:
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)
)
我们有类似于 ggplot 的东西吗 我们为每个变量定义颜色面板 ?
scale_manual <- function(...){
ggplot2::manual_scale(
"fill",
values = setnames(c("green","red","blue","yellow","grey"),
c("var1","var2","var3","var4","var5")),
...
)
}
虽然这个Q好像回答了,
但它在这里不起作用。
请考虑以下代表:
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))
由于有很多地块使用相同的数据, 颜色要一致
因此,尝试对每个变量进行硬编码。
在为闪亮的应用程序和绘图图表制作交互式图表之前,我通常使用 leaflet
包中的 Color mapping
函数:
使用您想要的调色板对 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)
)
您也可以手动对其进行硬编码:
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)
)