【问题标题】:How to change pie chart segment colors in plotly with r RColorBrewer custom colors如何使用 r RColorBrewer 自定义颜色更改饼图分段颜色
【发布时间】:2021-08-15 10:07:59
【问题描述】:

我是编码新手,这是我第一次在这里提出问题,如果这不符合正确的问题格式,请原谅我。
我正在尝试使用 R 将饼图颜色从默认更改为RColorBrewer,或者任何自定义颜色。到目前为止,我已经尝试了在这里找到的所有建议,但没有一个成功。
我当前的代码如下,默认为蓝色/橙色组合:

labels = c('Australia','Elsewhere')
values = c(199656, 82883)

fig <- plot_ly(type='pie', labels=labels, values=values,
               textinfo='label+percent',
               insidetextorientation='radial') 

theme(plot.title = element_text(hjust = 0.0))

fig <- fig %>% layout(title = "Logan Population by Birthplace 2016")

fig

【问题讨论】:

    标签: r colors plotly pie-chart


    【解决方案1】:

    您可以通过markercolors 属性设置自定义颜色。有关样式饼图的更多信息,请参阅here:

    library(plotly)
    
    labels <- c("Australia", "Elsewhere")
    values <- c(199656, 82883)
    
    cols <- RColorBrewer::brewer.pal(3, "Blues")[seq_along(labels)]
    
    plot_ly(
      type = "pie",
      labels = labels,
      values = values,
      marker = list(colors = cols),
      textinfo = "label+percent",
      insidetextorientation = "radial"
    ) %>%
      layout(title = "Logan Population by Birthplace 2016")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 2012-10-12
      相关资源
      最近更新 更多