【问题标题】:How to hard code the color for the displayed variable in plotly pie chart?如何在绘图饼图中硬编码显示变量的颜色?
【发布时间】:2019-12-27 11:11:54
【问题描述】:

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

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

虽然这个 Q 似乎回答了, 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))

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

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

【问题讨论】:

    标签: r plotly pie-chart


    【解决方案1】:

    在为闪亮的应用程序和绘图图表制作交互式绘图之前,我通常使用 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)
      )
    

    【讨论】:

    • 感谢您添加解决方案。目前,如果安装传单尝试了 install.packages("leaflet") 和 (!require('devtools')) install.packages('devtools') devtools::install_github('rstudio/leaflet') “png”问题包本身
    • @Abhi 传单是一个非常有用和流行的软件包。您可以手动安装其依赖项。你的Sys.info() 是什么?另外,我将通过编辑我的响应来添加另一种对颜色进行硬编码的方法。
    • 错误以 PNG 开头。 read.c:3:17:致命错误:png.h:没有这样的文件或目录#include ^ 编译终止。 make: *** [read.o] 错误 1 ​​错误: 包 'png' 编译失败 * 删除 install.packages 中的 '/home/myUserID/R/x86_64-redhat-linux-gnu-library/3.4/png' 警告:安装包'png'的退出状态非零错误:依赖'png'不适用于包'leaflet' *删除'/home/myuserID/R/x86_64-redhat-linux-gnu-library/3.4/leaflet ' install.packages 中的警告:安装包 'leaflet' 有非 zer
    • 关闭您的 rstudio 并且不保存工作区图像。在您的 rstudio (remove.packages("png", lib="~/home/myUserID/R/x86_64-redhat-linux-gnu-library/3.4")) 中删除 png 包并将其从您的 /home/myUserID/R/x86_64-redhat-linux-gnu-library/3.4/png 中删除。然后,install.packages("png")。如果成功,可以安装leaflet
    • 感谢您帮助我...。这是 png 的问题...。stackoverflow.com/questions/21800909/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多