Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Plotly set custom colors for bar chart

Tags:

r

shiny

plotly

I've got a plotly bar chart in my Shiny app, and I'd like to set specific colors each column in the resulting bar chart.

#Here's some reproducible data
df=data.frame(Month=c("Jan","Feb","Mar","Apr","May","Jun"),Criteria1=c(10,15,20,15,7,6),Criteria2=c(3,8,5,7,9,10),Criteria3=c(11,18,14,9,3,1))

#Plot

colNames <- names(df)[-1] #Month is the first column

# Here is where I set the colors for each `Criteria`, assuming that the order of colors follows the same order as the factor levels of the `Criteria`.

p <- plotly::plot_ly(marker=list(colors=c('#CC1480', '#FF9673', '#E1C8B4')))

for(trace in colNames){
  p <- p %>% plotly::add_trace(data = df, x = ~Month, y = as.formula(paste0("~`", trace, "`")), name = trace, type = "bar")
}

p %>% 
  layout(title = "Trend Over Time",showlegend = FALSE,
         xaxis = list(title = ""),
         yaxis = list (title = "Monthly Count of QoL Tweets"))

However the resulting plot does not show any of the colors I specify.

What am I doing incorrectly? Any pointers would be very appreciated.

like image 249
Varun Avatar asked Oct 16 '17 13:10

Varun


2 Answers

I don't think loop is neccessary here, the following provides as well more control over choosing the color for specific levels when df is melted, the individual levels Criteria1, Criteria2, Criteria3

library(plotly)
library(reshape2)

#Yout data.frame
df <- data.frame(Month = c("Jan","Feb","Mar","Apr","May","Jun"),
                 Criteria1 = c(10,15,20,15,7,6),
                 Criteria2 = c(3,8,5,7,9,10),
                 Criteria3 = c(11,18,14,9,3,1))

melt(df, id.vars = 'Month') %>% plot_ly(x = ~Month, y = ~value, type = 'bar', 
  color = ~variable,
  colors = c(Criteria1 = '#CC1480', Criteria2 = '#FF9673', Criteria3 = '#E1C8B4'))

enter image description here

like image 148
Patrik_P Avatar answered Oct 14 '22 12:10

Patrik_P


You could assign your colors to a vector:

colors <- c('#CC1480', '#FF9673', '#E1C8B4')

and then add the traces in a slightly modified loop.

p <- plotly::add_trace(p, 
                       x = df$Month, 
                       y = df[,trace], 
                       marker = list(color = colors[[match(trace, colNames)]]), 
                       name = trace, 
                       type = "bar")
}

which will give you the following graph

enter image description here

Complete code

library("plotly")
df=data.frame(Month=c("Jan", "Feb","Mar", "Apr", "May", "Jun"),
              Criteria1 = c(10, 15,20,15,7,6),
              Criteria2 = c(3, 8, 5, 7, 9, 10),
              Criteria3 = c(11, 18, 14, 9, 3, 1))

colNames <- names(df)[-1] #Month is the first column
colors <- c('#CC1480', '#FF9673', '#E1C8B4')
p <- plotly::plot_ly()

#colNames = c('Criteria1')
for(trace in colNames){
  p <- plotly::add_trace(p, 
                         x = df$Month, 
                         y = df[,trace], 
                         marker = list(color = colors[[match(trace, colNames)]]), 
                         name = trace, 
                         type = "bar")
}

p
like image 41
Maximilian Peters Avatar answered Oct 14 '22 12:10

Maximilian Peters