相同的颜色分配给 R 中相同水平的因子 plotly
Same color assigned to same level of factor in 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=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 每次都呈现相同的颜色。
我不确定这是否可行,但作为替代方案,您可以按照您的建议使用 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)
您也可以使用 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/
一个更新(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"))
我有一个闪亮的应用程序,其中包含几个情节可视化。用户可以选择多种产品,我希望每种产品在整个应用程序中都具有相同的独特颜色。我的一个可视化可以例如看起来像这样:
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 每次都呈现相同的颜色。
我不确定这是否可行,但作为替代方案,您可以按照您的建议使用 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)
您也可以使用 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/
一个更新(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"))