1
我需要爲barplot中的每個組手動設置顏色。我目前有填充=時間,這是目前確定的顏色。 我們有5個品牌和每個品牌2個獨立月份的值。我需要按品牌分組,但也需要一種方法來顯示哪個欄代表哪個月(時間),我目前可以做到這一點,但是我想爲每個欄組着色。例如。 brand1酒吧=紅色,brand2棒=藍色ECT,同時仍然有填補=時間在barplot ggplot中手動設置每個組的顏色
這裏是我的代碼:
colors <- c("#98999B", "#F4C400", "#CB003D", "#6BABE5", "#E65400", "#542C82")
time <- c("February 2017","March 2017","February 2017","March 2017","February 2017","March 2017","February 2017","March 2017","February 2017","March 2017")
value <- as.numeric(c("3.08","3.64","1.61","1.81","-1.02","-1.09","-5.23","-5.08","-1.51","-1.43"))
brand <- c("brand1","brand1","brand2","brand2","brand3","brand3","brand4","brand4","brand5","brand5")
Monthly_BMS_df <- as.data.table(cbind(time,value,brand))
bar <- ggplot(Monthly_BMS_df, aes(brand, value, fill = time)) +
geom_bar(stat="identity", position = "dodge") +
theme(legend.position='none') + scale_fill_manual(values=colors)
ggplotly(bar, width=1000,height=350)