在R中以相同的颜色分配给相同级别的因子 [英] Same color assigned to same level of factor in R plotly

查看:131
本文介绍了在R中以相同的颜色分配给相同级别的因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Shiny应用程序,具有多个图形化可视化效果.允许用户选择几种产品,我希望每种产品在整个应用程序中都具有相同的唯一颜色.我的可视化之一可以例如看起来像这样:

I have a Shiny app with several plotly visualisations. The user is allowed to choose several products and I would like each product to have the same unique color throughout the entire app. One of my visualisations could e.g. look like this:

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的第一价值?

Is it possible to make sure that the first level of Product ALWAYS get the first value of colpal?

在ggplot中,我认为这可以通过指定如下颜色的调色板来实现:

In ggplot I think this can be achieved by specifying the color pallette like this:

 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每次都呈现相同的颜色.

So I would like e.g. Product A to take on the same color everytime.

推荐答案

您也可以使用plotly制作调色板,但是它并不算太优雅,至少是我过去所做的那样.应该可以帮助您入门.这是一个示例:

You can make a colour palette with plotly as well, but it's not overly elegant, at least the way I've done it in the past. Should get you started though. Here's an example:

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原始颜色图上的任何新数据集,并使用此新颜色图.

The basic idea is to match any new data set on the original colour map and use this new colour map instead.

请注意,由于plotly使用的顺序(我有时无法解读),具有因子变量可能会弄乱这一点.

Note that have factor variables can mess this up due to the ordering that plotly uses (which I can't decipher sometimes).

关键会话信息:

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

...

本示例使用plotly3.x.x.如果您使用plotly 4.x.x或更高版本,则此代码可能无法按原样工作.详情请参阅此处: https://www. r-bloggers.com/upgradeing-to-plotly-4-0-and-above/

This example uses plotly 3.x.x. If you use plotly 4.x.x or above, this code may not work as is. See here for more details: https://www.r-bloggers.com/upgrading-to-plotly-4-0-and-above/

这篇关于在R中以相同的颜色分配给相同级别的因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆