highlighting and coloring differently a specific section of data using ggplot2

时间:2017-12-16 00:01:06

标签: r ggplot2

I have data and I have been able to put it into a ggplot graph (time series data). The data is over 12 years and there are specific spikes in the data for certain periods (the data is in weeks). I would like to try and color code one particular week of each year where the spikes begin but do not know where to begin.

The idea that I have is that the spike occurs in January when the superbowl happens!, that would be the week column 2001-01-01 - 2001-31-01 Is it possible to subset a period using ggplot and color code the graph accordingly. So for the superbowl week use a different color?

i.e. each year 2001 - 2012 color code Jan (01-01) to (01-31) red for example. That is 4 weeks of data. What I currently have is;

df[, .(df_sales = (sum(qty) * (EUR))), by = week] %>%
  ggplot(aes(x = week, y = df_sales)) +
  labs(x = 'wks', title = 'TS plot of qty x eur')

Which gives me a nice plot but I would like to color code the spikes (i.e. my hypothsis that they occur in January, week of the superbowl). I can post the graph for clarification if necessary.

     ID     unit  qty    NA    EUR     KEY      identity       week
1: 1123539 1147     1     GR    2.39  652159 10090100003 2001-08-20
2: 3102228 1129     1     GR    2.15  257871 10090100003 2001-04-16
3: 3321265 1129     1     GR    2.15  257871 10090100003 2001-04-16
4: 3321265 1122     1     GR    2.15  257871 10090100004 2001-02-26
5: 1120774 1151     1     GR    2.39  213290 10090100005 2001-09-17
6: 1145763 1157     1     GR    2.39  213290 10090100005 2001-10-29

EDIT: I attach the graph for clarification enter image description here

EDIT2:我附上新图enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将第二个geomsubset结合使用,如下所示:

library(lubridate)
ggplot(df, aes(x = week, y = df_sales)) + 
  geom_bar(stat = "identity") + 
  geom_bar(data = subset(df, month(week) == "1"), stat = "identity", col = "red") +
  labs(x = 'wks', title = 'TS plot of qty x eur') 

在这里,我们使用lubridate::month来检查哪一行属于一月份的一周。

对于一些虚构的随机数据:

enter image description here