本文介绍了在barplot ggplot中为每个组手动设置颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要为条形图中的每个组手动设置颜色.我目前有填充=时间,这目前正在确定颜色.我们有5个品牌,每个品牌分别有2个月的价值.我需要按品牌分组,但是还需要一种显示哪个条形表示哪个月(时间)的方法,我目前可以执行此操作,但是我想为每个条形组上色.例如. brand1条=红色,brand2条=蓝色ect,但仍有填充时间= p

I need to manually set colors for each group in a barplot. I currently have the fill = time and this is currently determining the colors.We have 5 brands and the values for 2 separate months per brand. I need to group by brand but also need a way to display which bar represents which month(time), I can currently do this however I want to color each bar group. eg. brand1 bars = red, brand2 bars = blue ect whilst still having fill = time

这是我的代码:

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)

推荐答案

一个选项是创建一个hcl调色板,每个brand的色调都不同,并且顺序亮度在每个月的每个月都相同不同的品牌.例如:

One option would be to create an hcl color palette with a different hue for each brand and a sequential luminosity that is the same for each month across different brands. For example:

library(ggplot2)
library(data.table)
library(plotly)

Monthly_BMS_df <- data.table(time, value, brand)

创建调色板:

nb = length(unique(Monthly_BMS_df$brand))
nm = length(unique(Monthly_BMS_df$time))

colors = apply(expand.grid(seq(70,40,length=nm), 100, seq(15,375,length=nb+1)[1:nb]), 1,
               function(x) hcl(x[3],x[2],x[1]))

在下面的代码中,我们使用fill=interaction(time, brand)将不同的颜色映射到品牌和月份的每种组合.然后scale_fill_manual分配我们在上面创建的调色板.每个月的亮度降低,因此3月比2月更暗.

In the code below, we use fill=interaction(time, brand) to map a different color to each combination of brand and month. Then scale_fill_manual assigns the color palette we created above. The luminosity decreases for each month so that March is darker than February.

bar <- ggplot(Monthly_BMS_df, aes(brand, value, fill=interaction(time, brand))) +
  geom_hline(yintercept=0, colour="grey60") +
  geom_bar(stat="identity", position = "dodge", show.legend=FALSE) +
  scale_fill_manual(values=colors) +
  theme_classic()

ggplotly(bar, width=1000, height=350)

作为上述图解的替代方法,线条图解可以使比较每个品牌的趋势变得更加容易.

As an alternative to the plot above, a line plot might make it easier to compare the trends in each brand.

library(dplyr)

ggplot(Monthly_BMS_df, aes(time, value, group=brand, colour=brand)) +
  geom_hline(yintercept=0, colour="grey60") +
  geom_text(data=Monthly_BMS_df %>% filter(time==min(time)),
            aes(label=brand), position=position_nudge(-0.25)) +
  geom_line(linetype="12", alpha=0.5, size=0.7) +
  geom_text(aes(label=value)) +
  guides(colour=FALSE) +
  theme_classic()

这篇关于在barplot ggplot中为每个组手动设置颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:19