简体   繁体   中英

Change bar colors in ggplotly() overlapping bar plot

I create the overlapping bar chart combining ggplot2 and ggplotly() . I want to set the bar colors to yellow and black but how can I set the colors?

library(plotly)
library(ggplot2)
library(reshape2)
Emails <- c("g", "o", "m")
Sent <- c(20, 24, 33)
Clicked <- c(12, 18, 29)
data <- data.frame(Emails, Sent, Clicked)


data %>%
melt(value.name = 'n',id.vars = 'Emails',variable.name = 'type')  %>%
ggplot(aes(x=Emails,y=n,fill=type))+
theme_bw()+
geom_col(position = position_jitterdodge(dodge.width = 0.5,jitter.height = 0,jitter.width = 0,seed = 25)) -> gg_output


plotly_object <- plotly::ggplotly(gg_output)

plotly_object

You could set your desired fill colors via scale_fill_manual :

library(plotly)

data %>%
  melt(value.name = 'n',id.vars = 'Emails',variable.name = 'type')  %>%
  ggplot(aes(x=Emails,y=n,fill=type))+
  scale_fill_manual(values = c("Sent" = "black", "Clicked" = "yellow")) +
  theme_bw()+
  geom_col(position = position_jitterdodge(dodge.width = 0.5,jitter.height = 0,jitter.width = 0,seed = 25)) -> gg_output

plotly_object <- plotly::ggplotly(gg_output)

plotly_object

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM