r - R - Plotly 几个调色板分组栏
问题描述
我有以下情节:
library(plotly)
library(dplyr)
ggplot2::diamonds %>% count(cut, clarity) %>%
plot_ly(x = ~cut, y = ~n, color = ~clarity,colors = 'Blues')
现在我只有一个调色板'蓝色'适用于所有组。如何自定义它,以便每组有一个调色板?例如,我想要调色板
- 'Fair' 级别的'Blues'
- '好'级别的'绿色'
- “非常好”级别的“红色”
- '高级'级别的'紫色'
- “理想”级别的“灰色”
解决方案
以下代码似乎适用于静态ggplot2
图:
library(tidyverse)
library(plotly)
library(RColorBrewer)
sPalette <- c("Blues", "Greens", "Reds", "Purples", "Greys") %>%
sapply(., function(x) brewer.pal(8, name = x)) %>%
as.vector
diamonds %>%
count(cut, clarity) %>%
ggplot(., aes(x = cut, y = n, fill = interaction(clarity, cut, sep = " - "))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = sPalette, guide = F) +
theme_minimal()
这是结果:
相应的plot_ly
代码产生的条形之间有很大的空间,我不确定为什么会这样:
diamonds %>%
count(cut, clarity) %>%
plot_ly(x = ~cut, y = ~n, color = ~interaction(clarity, cut, sep = " - ") , colors = sPalette)
然而事实证明,这ggplotly
确实有效:
p <- diamonds %>%
count(cut, clarity) %>%
ggplot(., aes(x = cut, y = n, fill = interaction(clarity, cut, sep = " - "))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = sPalette, guide = F) +
theme_minimal()
ggplotly(p)
推荐阅读
- php - 为什么在此处出现错误“注意:未定义的索引:第 12 行 C:\xampp\htdocs\mobile-shope\reg.php 中的文件”?
- r - 用扫帚整理“火车”物体
- java - 如何在跟踪重复数的同时删除数组列表中的重复元素?JAVA
- ros - Gazebo 模拟 PR2 机器人最大速度不被尊重
- html - 如何在 HTML/CSS 中使用模糊功能
- python - 导入 DataFrames - 没有这样的文件或目录
- c++ - 计数排序直到行尾
- r - 使用 highcharts R 开启/关闭时间序列
- angular - 为什么组件不渲染服务模型?
- python - AttributeError:模块'PIL'没有属性'Image'