简体   繁体   中英

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.

Preferably a ggplot way of doing this rather than preprocessing the data.

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)

If I understand correctly your request, you can try to :

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

You can specify any color you want in the scale_fill_manual() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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