更改 ggplotly() 重叠条形图中的条形颜色
Posted
技术标签:
【中文标题】更改 ggplotly() 重叠条形图中的条形颜色【英文标题】:Change bar colors in ggplotly() overlapping bar plot 【发布时间】:2021-10-23 21:11:06 【问题描述】:我创建了结合ggplot2
和ggplotly()
的重叠条形图。我想将条形颜色设置为黄色和黑色,但如何设置颜色?
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
【问题讨论】:
【参考方案1】:您可以通过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
【讨论】:
以上是关于更改 ggplotly() 重叠条形图中的条形颜色的主要内容,如果未能解决你的问题,请参考以下文章