1

I want to highlight some days in my time series ggplot. But I don't know how to highlight all.

I have this code:

library(ggplot2)
library(dplyr)

highlight_max <- canteen_menu_data %>%  
filter(date == c("2016-10-04","2017-09-27"))

highlight_min <- canteen_menu_data %>% 
filter(date == c("2016-12-27","2017-07-28"))

highlight_christmas2016 <- canteen_menu_data %>% 
filter(date == c("2016-12-21","2016-12-30"))

highlight_christmas2017 <- canteen_menu_data %>% 
filter(date == c("2017-12-18","2017-12-21"))

dput(canteen_menu_data) %>% 
  ggplot(aes(x=date,y=sales)) + 
  geom_line(alpha=0.3) +
  geom_point(data=highlight_max, aes(x=date,y=sales), color='red',size=3) +
  geom_point(data=highlight_min, aes(x=date,y=sales), color='red',size=3) +
  geom_point(data=highlight_christmas2016, aes(x=date,y=sales), color='darkred',size=1) +  
  geom_point(data=highlight_christmas2017, aes(x=date,y=sales), color='darkred',size=1)

When I run this, all the point are highlighted in my plot but the last line doesn't do anything as you can see in the picture

Daily sales

Can you please help me?

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • hi, your code is not [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) because we don't have access to your data (```canteen_menu_data```). You can add a sample of your data in your post with ```dput()``` or use a dataset included in base R, like ```mtcars``` or ```iris``` (these are not useful for time series but surely there are some datasets for this that are available for everybody) – bretauv Jan 25 '20 at 14:19
  • can you reproduce it now? – Mara Marques Jan 25 '20 at 14:30
  • Perhaps those dates don't exist in your data. – Andrew Gustar Jan 25 '20 at 14:34
  • @AndrewGustar the dates exist – Mara Marques Jan 25 '20 at 14:39
  • Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Jan 25 '20 at 17:40

1 Answers1

2

I think you made an error in the filtering, if you have two or more conditions, you should use %in% or specify with "|", for example,

highlight_max <- canteen_menu_data %>%  
filter(date == c("2016-10-04","2017-09-27"))

Should be:

filter(date %in% c("2016-10-04","2017-09-27"))

or:

filter(date == "2016-10-04"| date == "2017-09-27")

Also I am not sure if your date column is a character or Date. Please check using str(canteen_menu_date). We can simulate some data below, and I set the date column as should be able to reproduce the correct points:

set.seed(999)
canteen_menu_data = data.frame(
date = seq(as.Date("2016-10-01"),as.Date("2017-12-31"),by="day"),
sales = runif(457,min=100,max=400)
)

highlight_max <- canteen_menu_data %>%  
filter(date %in% as.Date(c("2016-10-04","2017-09-27")))

highlight_min <- canteen_menu_data %>% 
filter(date %in% as.Date(c("2016-12-27","2017-07-28")))

highlight_christmas2016 <- canteen_menu_data %>% 
filter(date %in% as.Date(c("2016-12-21","2016-12-30")))

highlight_christmas2017 <- canteen_menu_data %>% 
filter(date %in% as.Date(c("2017-12-18","2017-12-21")))

canteen_menu_data %>% 
  ggplot(aes(x=date,y=sales)) + 
  geom_line(alpha=0.3) +
  geom_point(data=highlight_max, aes(x=date,y=sales), color='red',size=3) +
  geom_point(data=highlight_min, aes(x=date,y=sales), color='red',size=3) +
  geom_point(data=highlight_christmas2016, aes(x=date,y=sales), color='darkred',size=1) +  
  geom_point(data=highlight_christmas2017, aes(x=date,y=sales), color='darkred',size=1)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Just one more question @StupidWolf. How can I label the point in the graph? – Mara Marques Jan 26 '20 at 15:17
  • To the code above, you add something like + geom_label(data=highlight_max, aes(x=date,y=sales,label="highlight_max"), nudge_x=20,nudge_y=20,label.size =0.1). So you need to play with the nudge_x and nudge_y to get a good visualization. Otherwise I suggest looking at the package ggrepel, with geom_text_repel – StupidWolf Jan 26 '20 at 21:33