[英]R - Plotly several color palettes grouped bar
I have the following plot with plotly :我有以下情节与 plotly :
library(plotly)
library(dplyr)
ggplot2::diamonds %>% count(cut, clarity) %>%
plot_ly(x = ~cut, y = ~n, color = ~clarity,colors = 'Blues')
Right now I only have one color palette 'Blues' for all groups.现在,我只有一个适用于所有组的调色板“蓝调”。 How can i customize it so I have one color palette per group ?
我如何自定义它以便每组有一个调色板? For example, I would like the color palette
例如,我想要调色板
The following code seems to work with a static ggplot2
plot:以下代码似乎适用于静态
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()
This is the result:这是结果:
The corresponding plot_ly
code produces bars which have a wide space between them, and I'm not exactly sure why that's the case:相应的
plot_ly
代码生成的条形之间有很大的空间,我不确定为什么会这样:
diamonds %>%
count(cut, clarity) %>%
plot_ly(x = ~cut, y = ~n, color = ~interaction(clarity, cut, sep = " - ") , colors = sPalette)
It turns out however, that ggplotly
does work:然而,事实证明
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.