我有一个闪亮的应用程序,有几个情节可视化。用户可以选择多种产品,我希望每种产品在整个应用程序中具有相同的独特颜色。我的一个可视化可能是看起来像这样:
plot_ly(df, x = variable, y=value, type = "bar", color = Product, hoverinfo = "text",
colors = colpal, text = paste0(df$value,"%")) %>%
layout(xaxis=ax, yaxis=yx, legend=list(x=1, y = 0.5))
是否有可能确保产品的第一级始终获得colpal的第一个值?
在ggplot中我认为这可以通过指定颜色调色板来实现:
c("Product A" = "#00AADC", "Product B" = "#843532","Product C" = "#2C5481", "Product D" = "#CADFE1")
但这似乎并没有在情节上发挥作用。
非常感谢任何帮助。
编辑:样本数据集
Product variable value
1 Product A DDD 24
2 Product B DDD 22
3 Product C DDD 35
4 Product D DDD 19
5 Product A Brand attention 29
6 Product B Brand attention 27
7 Product C Brand attention 27
8 Product D Brand attention 18
所以我想要,例如产品A每次都采用相同的颜色。
答案 0 :(得分:1)
您也可以使用plotly
制作调色板,但它并不过分优雅,至少我过去的方式是这样做的。应该让你开始吧。这是一个例子:
library(plotly)
# Create a colour map
# Note that using factors will mess this up
mapColours <- data.frame(x = c('Graham', 'Eric', 'Terry', 'John'),
colours = c('green', 'blue', 'red', 'orange'),
stringsAsFactors = FALSE)
# The full data to plot
df <- data.frame(x = mapColours$x,
y = c(7, 9, 5, 8),
stringsAsFactors = FALSE)
# Plot all categories
plot_ly(df, x = x, y = y, type = 'bar', color = x, colors = mapColours$colours)
# Now subset the data
dfSub <- subset(df, subset = x %in% c('Eric', 'John'))
dfSub <- droplevels(dfSub)
# Won't work as is, uses the wrong colours
plot_ly(dfSub, x = x, y = y, type = 'bar', color = x, colors = mapColours$colours)
# Need to get new colour map
mapColoursSub <- mapColours[match(dfSub$x, mapColours$x), 'colours']
# Use the subsetted colour map
plot_ly(dfSub, x = x, y = y, type = 'bar', color = x, colors = mapColoursSub)
基本思路是match
原始色彩图上的任何新数据集,而是使用这个新的色彩图。
请注意,由于plotly
使用的排序(有时我无法解密),因子变量可能会导致这种情况混乱。
关键会话信息:
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
...
other attached packages:
[1] plotly_3.4.3 ggplot2_2.1.0
...
编辑:
此示例使用plotly
3.x.x.如果您使用plotly
4.x.x或更高版本,则此代码可能无法正常工作。有关详情,请参阅此处:https://www.r-bloggers.com/upgrading-to-plotly-4-0-and-above/
答案 1 :(得分:0)
我不确定是否可能,但作为替代方案,您可以使用ggplot2。
如果你下载了plotly的开发版本(否则它将无法工作),你可以试试这个:
devtools::install_github("ropensci/plotly")
library(plotly)
df <- read.table(text="
Product variable value
1 ProductA DDD 24
2 ProductB DDD 22
3 ProductC DDD 35
4 ProductD DDD 19
5 ProductA Brandattention 29
6 ProductB Brandattention 27
7 ProductC Brandattention 27
8 ProductD Brandattention 18",
header = TRUE)
p <- ggplot(data=df, aes(x=variable, y=value, fill=Product)) + geom_bar(stat='identity', position='dodge') + scale_fill_manual(values=c("ProductA" = "red", "ProductB" = "black","ProductC" = "blue", "ProductD" = "orange"))
ggplotly(p)
答案 2 :(得分:0)
一项更新(2019年末)可以在普通绘图中使用,对条形绘图使用'color'和'colors'属性:
library(plotly)
df <- read.table(text="
Product variable value
1 ProductA DDD 24
2 ProductB DDD 22
3 ProductC DDD 35
4 ProductD DDD 19
5 ProductA Brandattention 29
6 ProductB Brandattention 27
7 ProductC Brandattention 27
8 ProductD Brandattention 18", header = TRUE)
df %>% plot_ly(x = ~variable, y = ~value, type = 'bar',
color = ~Product,
colors = c("ProductA" = "red",
"ProductB" = "black",
"ProductC" = "blue",
"ProductD" = "orange"))