简体   繁体   English

如何为堆叠的条形图中的两列着色

[英]How to colour two columns in a stacked barplot

Hello stack overflow community, 你好堆栈溢出社区,

I'm trying to find a way to colour two columns that I managed to put together in a stacked barplot. 我正在尝试找到一种方法来对两列进行着色,这些两列我已设法将它们组合成一个堆叠的条形图。 The X axis represents the months, and the columns represent both the year (there is 'n-1' year and 'n' year) and type of business. X轴代表月份,列代表年份(有“ n-1”年和“ n”年)和业务类型。 Type of business was easy to colour because it is the fill, but I can't find a way to have different colours for 'n-1' year and 'n' year columns, or a way to clarify that each column per month represents a different year. 业务类型很容易上色,因为它是填充,但是我找不到找到“ n-1”年和“ n”年列具有不同颜色的方法,或者是澄清每个月的每个列代表的颜色的方法。不同的一年。

For clarity purposes, this is what the data looks like: 为了清楚起见,数据如下所示: 在此处输入图片说明

This is the code I used so far: 这是我到目前为止使用的代码:

barwidth = 0.35
ggplot() + 
  geom_bar(data = BUSINESS_18,
           mapping = aes(x=Month, y=GWP_mio, fill=Business),
           stat ="identity",
           position = "stack",
           width = barwidth) +        
  geom_bar(data = BUSINESS_19,
         mapping = aes(x=as.numeric(Month) + as.numeric(barwidth) + 0.05, y=GWP_mio, fill=Business),
         stat ="identity",
         position = "stack",
         width = barwidth) +
  theme_ipsum() +
  scale_fill_manual(values = c("#009E73","Darkblue")) +
  scale_y_continuous(breaks=seq(0,18000000,1000000), labels = scales::comma_format(scale = 1/1000000, 
  accuracy = .1), limits = c(0,18000000)) +
  ggtitle('GWP development') +
  theme(plot.title = element_text(hjust=0.5, size=14, family="Calibri", face="bold"),
        legend.title = element_text(size=11, family="Calibri", face="bold"),
        axis.title.x = element_text(hjust=1, size=11, family="Calibri", face="bold"),
        axis.text.x = element_text(angle=0, hjust=1, size=11, family="Calibri"),
        axis.title.y = element_text(angle=0, hjust=1, size=10, family="Calibri", face="bold"))

And a picture of the graph, so you can more easily understand what I mean (no differentiation between the two columns per month): 还有一张图形图片,这样您就可以更轻松地理解我的意思(每月两列之间无区别): 在此处输入图片说明 Any help would be highly appreciated! 任何帮助将不胜感激! Many thanks in advance. 提前谢谢了。

Using a facet layer here would be a better approach, but at this point I am pretty sure you already know how to do that. 在此处使用构面层是一种更好的方法,但是到现在为止,我很确定您已经知道如何做到这一点。

My recomendation is to create a new variable, pasting together yera and business and using this one as the fill aesthetic. 我的建议是创建一个新变量,将yerabusiness粘贴在一起,并将其用作填充美学。

data 数据

df <- data.frame(
  year = rep(c(2017, 2018), each = 24),
  gwp = rnorm(48, mean = 10000, sd = 300),
  month = rep(1:12, 4),
  business = rep(
    c(
      rep("new", 12),
      rep("renewal", 12)
    )
  )
)

df <- df %>%
  mutate(
    fill = paste(business, year, sep = "-")
  )

plot 情节

  ggplot() +
  geom_col(
    data = df %>% filter(year == 2018),
    aes(x = month, fill = fill, y = gwp),
    width = barwidth
  ) +
    geom_col(
      data = df %>% filter(year == 2017),
      aes(x = month + as.numeric(barwidth) + 0.05, fill = fill, y = gwp),
      width = barwidth
    )

在此处输入图片说明

If you create a factor with the new variable and order the legend labels as your wish. 如果您使用新变量创建一个因子,并根据需要对图例标签进行排序。

Another way to do this would be controlling the apha, you can see how to do it in the answer to this question 另一种方法是控制apha,您可以在此问题的答案中看到如何做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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