[英]Adding Custom Colors to Plotly Bar Chart with ColorRamp
I am using Plotly to make charts and graphs. 我正在使用Plotly制作图表。 Currently I am trying to apply a custom color to my graph, but the graph has more colors than the ColorBrewer palette offers.
目前,我正在尝试将自定义颜色应用于图形,但是图形比ColorBrewer调色板提供的颜色更多。
Here is my data: https://github.com/kffont/Colorado/blob/master/data1.csv 这是我的数据: https : //github.com/kffont/Colorado/blob/master/data1.csv
The data is the min, median and max of 5 number summaries for 15 rows of data. 数据是15行数据的5个数字汇总的最小值,中位数和最大值。 The 16th row identifies the categorical labels (min, med, max).
第16行标识类别标签(最小,中,最大)。
I used Color Ramp to create a function to allow more colors to be used in my bar plot. 我使用了“色彩斜坡”来创建一个函数,以允许在我的条形图中使用更多的颜色。 That is working fine.
很好 The problem occurs when I try and assign the colors to the plot.
当我尝试将颜色分配给绘图时会发生问题。 I've used many solutions to no avail.
我使用了许多解决方案都无济于事。 Here are some of the methods I've tried:
这是我尝试过的一些方法:
create a vector of colnames, color = colnames colors = color palette 创建向量的colnames,颜色= colnames colors =调色板
Assign colors with palettename[1] etc using indexing 使用索引为调色板名称[1]等分配颜色
remove the traces and create 2 lines of code; 删除痕迹并创建两行代码; 1 for the plot and one for the layout.
1用于绘图,一个用于布局。 I tried this method with and without the color/colors method in #1
我在#1中尝试使用和不使用颜色/颜色方法的方法
Here is the base code that I've used. 这是我使用的基本代码。
#Libraries
library(plotly)
library(colorRamps)
library(RColorBrewer)
#Create palette col.a.cat1<-colorRampPalette(brewer.pal(9,"YlOrRd"))
#Color = Colnames
abc<-colnames(data1)
#Barcharts #1 (p) and #2 (o)
p<-plot_ly(a.cat.14, x = ~Level, y = ~a.cat.14[, 1],, type = 'bar', name = colnames(a.cat.14[1])) %>%
add_trace(y = ~a.cat.14[, 2], name = colnames(a.cat.14[2])) %>%
add_trace(y = ~a.cat.14[, 3], name = colnames(a.cat.14[3])) %>%
add_trace(y = ~a.cat.14[, 4], name = colnames(a.cat.14[4])) %>%
add_trace(y = ~a.cat.14[, 5], name = colnames(a.cat.14[5])) %>%
add_trace(y = ~a.cat.14[, 6], name = colnames(a.cat.14[6])) %>%
add_trace(y = ~a.cat.14[, 7], name = colnames(a.cat.14[7])) %>%
add_trace(y = ~a.cat.14[, 8], name = colnames(a.cat.14[8])) %>%
add_trace(y = ~a.cat.14[, 9], name = colnames(a.cat.14[9])) %>%
add_trace(y = ~a.cat.14[, 10], name = colnames(a.cat.14[10])) %>%
add_trace(y = ~a.cat.14[, 11], name = colnames(a.cat.14[11])) %>%
add_trace(y = ~a.cat.14[, 12], name = colnames(a.cat.14[12])) %>%
add_trace(y = ~a.cat.14[, 13], name = colnames(a.cat.14[13])) %>%
add_trace(y = ~a.cat.14[, 14], name = colnames(a.cat.14[14])) %>%
add_trace(y = ~a.cat.14[, 15], name = colnames(a.cat.14[15])) %>%
layout(title = "Adult Cats: Minimum, Median, Maximum", yaxis = list(title= 'Count'), barmode = 'group')
p
o<-plot_ly(a.cat.14, x = ~Level, y = ~a.cat.14[, c(1:15)], type = 'bar', name = colnames(a.cat.14)) %>%
layout(title = "Adult Cats: Minimum, Median, Maximum", yaxis = list(title= 'Count'), barmode = 'group')
Your help is appreciated! 感谢您的帮助!
colorRampPalette
needs an 'lower' and 'upper' color, in your case probably yellow and red. colorRampPalette
需要“下”和“上”的颜色,在您的情况下可能是黄色和红色。
colorRampPalette(list('yellow', 'red'))
When you call the returned function the provided numeric argument is the number of colors to return 调用返回的函数时,提供的数字参数是要返回的颜色数
colors <- colorRampPalette(list('yellow', 'red'))(15)
When can then assign the colors to Plotly
然后何时可以将颜色分配给
Plotly
p <- plot_ly(x = x, y = y, marker = list(color = colors), type='bar')
#Libraries
library(plotly)
library(colorRamps)
library(RColorBrewer)
#read csv
csv <- read.csv(url("https://raw.githubusercontent.com/kffont/Colorado/master/data1.csv"))
col_names <- colnames(csv)
#create palette
colors <- colorRampPalette(list('yellow', 'red'))(length(col_names))
#get y values
y <- list()
for (i in 1:length(col_names))
{
y[[i]] = csv[[i]][3]
}
p <- plot_ly(x = col_names[2:15], y = y[2:15], marker = list(color = colors[2:15]), type='bar')
p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.