I am using Plotly to make charts and graphs. Currently I am trying to apply a custom color to my graph, but the graph has more colors than the ColorBrewer palette offers.
Here is my data: 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. The 16th row identifies the categorical labels (min, med, max).
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
Assign colors with palettename[1] etc using indexing
remove the traces and create 2 lines of code; 1 for the plot and one for the layout. I tried this method with and without the color/colors method in #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(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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.