简体   繁体   English

geom_rect:每个季节重复的背景颜色

[英]geom_rect: background color repeated per season

I have a dataframe like this:我有一个这样的数据框:

df<-data.frame(Category= c("a","b","a","b"), Value = c(25,90,40,10), Date= c("2016-02-13", "2016-05-13", "2016-08-13", "2016-11-13"))

In reality it is more complex, has several years and several observed objects so that it should be a faceted plot in the end, but I think this has nothing to do with the question.实际上它更复杂,有几年和几个观察对象,所以它最终应该是一个多面的情节,但我认为这与问题无关。

I want to have a ggplot (line plot), where every season got it's own background color.我想要一个 ggplot (线图),每个季节都有自己的背景颜色。 eg: spring from March to May in yellow, summer from June to August in red autumn from September to November in blue and winter from December to February in grey.例如:春季三月至五月为黄色,夏季六月至八月为红色,秋季九月至十一月为蓝色,冬季十二月至二月为灰色。

This should be repeated, regardless the year as it goes through several years and the database will be updated with time.这应该重复,无论年份如何,因为它经过了几年,并且数据库将随着时间的推移而更新。

I tried a lot with geom_rect but didn't find a working solution.我用 geom_rect 做了很多尝试,但没有找到可行的解决方案。

Thanks for any advice!感谢您的任何建议!

If I understand your goal correctly, I think you can achieve it by creating two additional variables, say Season and Color that correspond to Date column, and then supply the columns as necessary to geom_line .如果我正确理解您的目标,我认为您可以通过创建两个额外的变量来实现它,例如对应于Date列的SeasonColor ,然后根据需要将列提供给geom_line

To make the steps and the results clearer, I create a dummy data by expanding your data to another year (2017) with similar date and category but slightly different values:为了使步骤和结果更清晰,我通过将您的数据扩展到另一年(2017 年)来创建一个虚拟数据,该年份和类别具有相似的日期和类别但值略有不同:

1. The data一、数据

df<-data.frame(Category= c("a","b","a","b"), Value = c(25,90,40,10), Date= c("2016-02-13", "2016-05-13", "2016-08-13", "2016-11-13"))
df2<-data.frame(Category= c("a","b","a","b"), Value = c(30,95,45,15), Date= c("2017-02-13", "2017-05-13", "2017-08-13", "2017-11-13"))
dat <- rbind.data.frame(df,df2)

dat
  Category Value       Date
1        a    25 2016-02-13
2        b    90 2016-05-13
3        a    40 2016-08-13
4        b    10 2016-11-13
5        a    30 2017-02-13
6        b    95 2017-05-13
7        a    45 2017-08-13
8        b    15 2017-11-13

2. Creating Season and Color columns 2. 创建SeasonColor

dat.season <- dat %>% 
     mutate(Date = as.Date(Date)) %>% 
     mutate(Month = months(Date)) %>% 
     mutate(Season = case_when(Month %in% c("March", "April", "May") ~ "spring",  
                               Month %in% c("June", "July", "August") ~ "summer", 
                               Month %in% c("September", "October", "November") ~ "autumn", 
                               Month %in% c("December", "January", "February")~ "winter")) %>% 
     mutate(Color = case_when(Season == "spring"~ "yellow", 
                              Season == "summer"~ "red", 
                              Season == "autumn"~ "blue", 
                              Season == "winter"~ "grey"))
dat.season
  Category Value       Date    Month Season  Color
1        a    25 2016-02-13 February winter   grey
2        b    90 2016-05-13      May spring yellow
3        a    40 2016-08-13   August summer    red
4        b    10 2016-11-13 November autumn   blue
5        a    30 2017-02-13 February winter   grey
6        b    95 2017-05-13      May spring yellow
7        a    45 2017-08-13   August summer    red
8        b    15 2017-11-13 November autumn   blue

Supplying the columns to geom_line()将列提供给geom_line()

dat.season %>% ggplot() + 
  geom_line(aes(x = Date, y = Value), 
            colour = dat.season$Color) + 
  theme_bw()

The result结果

在此处输入图像描述

Update to add coloured background更新以添加彩色背景

Here is the line plot along with coloured backgrounds for each season.这是线图以及每个季节的彩色背景。

dat.season %>% 
    ggplot() + 
    geom_rect(aes(xmin = Date[1], xmax = Date[2], 
                  ymin = Value[1], ymax = Value[2]), 
              fill = dat.season$Color[1])+
    geom_rect(aes(xmin = Date[2], xmax = Date[3], 
                  ymin = Value[2], ymax = Value[3]), 
              fill = dat.season$Color[2])+
    geom_rect(aes(xmin = Date[3], xmax = Date[4], 
                  ymin = Value[3], ymax = Value[4]), 
              fill = dat.season$Color[3])+
    geom_rect(aes(xmin = Date[4], xmax = Date[5], 
                  ymin = Value[4], ymax = Value[5]), 
              fill = dat.season$Color[4])+
    geom_rect(aes(xmin = Date[5], xmax = Date[6], 
                  ymin = Value[5], ymax = Value[6]), 
              fill = dat.season$Color[5])+
    geom_rect(aes(xmin = Date[6], xmax = Date[7], 
                  ymin = Value[6], ymax = Value[7]), 
              fill = dat.season$Color[6])+
    geom_rect(aes(xmin = Date[7], xmax = Date[8], 
                  ymin = Value[7], ymax = Value[8]), 
              fill = dat.season$Color[7])+
    geom_line(aes(x = Date, y = Value)) +
    theme_bw()

The result结果

在此处输入图像描述

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

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