简体   繁体   English

如何通过ggplot geom_bar中的组来填充

[英]How to fill by mean of a group in ggplot geom_bar

I am trying to fill my bar with colour scale showing the mean of the group. 我正在尝试用显示组平均值的色标填充我的栏。 The output is showing only one shade of grey . 输出仅显示一种阴影。

How can I make this work to have the fill of each bar represent the mean of the CapacityFactor for each year. 我如何进行这项工作以使每个条形的填充都代表每年的CapacityFactor平均值。

Preferably a ggplot way of doing this rather than preprocessing the data. 最好采用ggplot方式进行此操作,而不是对数据进行预处理。

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

metric004 <- data.frame(stringsAsFactors=FALSE,
            StationID = c("LOYYB", "LOYYB", "LOYYB", "LOYYB", "LOYYB", "LOYYB",
                          "LOYYB", "LOYYB", "OAKEY", "OAKEY", "OAKEY", "OAKEY",
                          "OAKEY", "OAKEY", "OAKEY", "OAKEY"),
                 DUID = c("LOYYB1", "LOYYB1", "LOYYB1", "LOYYB1", "LOYYB2",
                          "LOYYB2", "LOYYB2", "LOYYB2", "OAKEY1", "OAKEY1",
                          "OAKEY1", "OAKEY1", "OAKEY2", "OAKEY2", "OAKEY2",
                          "OAKEY2"),
           MonthStart = c(ymd("2017-11-01", "2017-12-01", "2018-01-01", "2018-02-01",
                          "2017-11-01", "2017-12-01", "2018-01-01",
                          "2018-02-01", "2017-11-01", "2017-12-01", "2018-01-01",
                          "2018-02-01", "2017-11-01", "2017-12-01", "2018-01-01",
                          "2018-02-01")),
       CapacityFactor = c(0.888504, 0.880521, 0.881841, 0.883836, 0.888695,
                          0.880279, 0.887361, 0.884969, 0, 0.027732, 0.007677,
                          0.038401, 0, 0.004178, 0.008861, 0.013907)
    )
m04_10y_faceted <- metric004 %>% group_by(StationID) %>%
    nest() %>%
    mutate(metricPlot = map(.x = data,
        ~ggplot(data=.x %>% group_by(year = as.Date(cut.Date(MonthStart, "year")))) +
            aes(year, CapacityFactor, fill = CapacityFactor) +
            stat_summary(fun.y = mean, na.rm = TRUE, geom = "bar") +
            scale_fill_continuous(limits=c(0,1)) +
            facet_grid(DUID ~ .) +
            xlab("Year") +
            ylab("Capacity Factor") +
            scale_x_date(date_breaks = "1 year", date_labels = "%Y", minor_breaks = NULL) +
            scale_y_continuous(limits=c(0, 1)) +
            coord_cartesian(xlim=c(ymd('2008-09-01'), ymd('2018-03-30'))) +
            theme_minimal() +
            theme(axis.title.x = element_blank(), axis.ticks = element_blank(),
                legend.position = "none")
    ))

walk2(m04_10y_faceted$StationID, m04_10y_faceted$metricPlot, function(.x, .y) {
    plot(.y)
})

从OP的链接https://i.imgur.com/jevVETq.png

Created on 2019-02-15 by the reprex package (v0.2.1) reprex软件包 (v0.2.1)创建于2019-02-15

If I understand correctly your request, you can try to : 如果我正确理解您的要求,则可以尝试:

  1. change the fill argument : aes(year, CapacityFactor, fill = as.factor(year(MonthStart))) 更改fill参数: aes(year, CapacityFactor, fill = as.factor(year(MonthStart)))
  2. change the scale_fill_continuous() into scale_fill_manual(values = c("blue", "red")) scale_fill_continuous()更改为scale_fill_manual(values = c("blue", "red"))

You can specify any color you want in the scale_fill_manual() . 您可以在scale_fill_manual()指定所需的任何颜色。

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

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