Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R图中为相同级别的因子指定相同的颜色_R_Plotly - Fatal编程技术网

在R图中为相同级别的因子指定相同的颜色

在R图中为相同级别的因子指定相同的颜色,r,plotly,R,Plotly,我有一个闪亮的应用程序,有几个生动的可视化效果。用户可以选择几个产品,我希望每个产品在整个应用程序中都有相同的独特颜色。例如,我的一个想象可能是这样的: 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=

我有一个闪亮的应用程序,有几个生动的可视化效果。用户可以选择几个产品,我希望每个产品在整个应用程序中都有相同的独特颜色。例如,我的一个想象可能是这样的:

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))
 c("Product A" = "#00AADC", "Product B" = "#843532","Product C" = "#2C5481", "Product D" = "#CADFE1")
是否可以确保产品的第一级始终获得colpal的第一个值

在ggplot中,我认为可以通过如下指定颜色托盘来实现:

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))
 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每次都采用相同的颜色。

我不确定这是否可行,但作为替代方案,您可以按照建议使用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)
devtools::install\u github(“ropensci/plotly”)
图书馆(绘本)

df你也可以用
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)
编辑: 本例使用
plotly
3.x.x。如果使用
plotly
4.x.x或更高版本,此代码可能无法正常工作。有关更多详细信息,请参见此处:

在普通绘图中使用“颜色”和“颜色”属性的更新(2019年底):

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"))
library(plotly)
df%plot_ly(x=~变量,y=~值,类型='bar',
颜色=~产品,
颜色=c(“产品A”=“红色”,
“产品B”=“黑色”,
“ProductC”=“蓝色”,
“产品D”=“橙色”))

你能让你的帖子重现吗?如果没有数据集,很难重现您的问题。@MLavoie请参阅编辑。谢谢。不幸的是,ggplot不支持Shining应用程序中的其他一些可视化,我希望将所有内容都放在一个框架内。