簡體   English   中英

geom_rect:每個季節重復的背景顏色

[英]geom_rect: background color repeated per season

我有一個這樣的數據框:

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"))

實際上它更復雜,有幾年和幾個觀察對象,所以它最終應該是一個多面的情節,但我認為這與問題無關。

我想要一個 ggplot (線圖),每個季節都有自己的背景顏色。 例如:春季三月至五月為黃色,夏季六月至八月為紅色,秋季九月至十一月為藍色,冬季十二月至二月為灰色。

這應該重復,無論年份如何,因為它經過了幾年,並且數據庫將隨着時間的推移而更新。

我用 geom_rect 做了很多嘗試,但沒有找到可行的解決方案。

感謝您的任何建議!

如果我正確理解您的目標,我認為您可以通過創建兩個額外的變量來實現它,例如對應於Date列的SeasonColor ,然后根據需要將列提供給geom_line

為了使步驟和結果更清晰,我通過將您的數據擴展到另一年(2017 年)來創建一個虛擬數據,該年份和類別具有相似的日期和類別但值略有不同:

一、數據

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. 創建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

將列提供給geom_line()

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

結果

在此處輸入圖像描述

更新以添加彩色背景

這是線圖以及每個季節的彩色背景。

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()

結果

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM