在 ggplotly() 重叠条形图中更改条形颜色

Change bar colors in ggplotly() overlapping bar plot

我创建了组合 ggplot2ggplotly() 的重叠条形图。我想将条形颜色设置为黄色和黑色,但如何设置颜色?

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

您可以通过 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