0

I am really new to R and trying to lean it myself but now I got stuck in a place. I have tried to add a shaded area to a facet but it adds a shaded area to every facet. I wanna highlight a time from 2017-08-08 to 2017-08-16 only and I don't know how to do it.

The code I have right now looks like this:

ggplot(data = Andmed3, aes(x= Aeg, y = arch, group = Prooviala)) + 
  stat_summary(geom = "errorbar",fun.data = mean_se, aes(group = Prooviala, color = Prooviala)) + 
  stat_summary(fun = mean,geom="line",lwd=1,aes (group= Prooviala, color = Prooviala)) + 
  stat_summary(geom = "point", fun = mean, shape = 16, size = 2, aes(color = Prooviala)) + 
  facet_grid(~Periood, scales = "free_x", space = "free_x") + 
  labs(x = "Kuupäev", y = "Arhede 16S rRNA geenikoopiate arv")+ 
  theme_bw() +  
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) + 
  scale_x_datetime(date_breaks = "1 week", date_labels = " %e %b %y") + 
  geom_rect(aes(xmin = as.POSIXct('2017-08-08', format = '%Y-%m-%d'),xmax = as.POSIXct('2017-08-16', format = '%Y-%m-%d'),ymin = -Inf,ymax = Inf), alpha = 0.2, fill = 'grey')

enter image description here

I need my graph to look like my second picture but just with the highlighted background in the first facet.

enter image description here

Can someone help me with the code?

Thank You!

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 1
    Welcome to SO! [This post](https://stackoverflow.com/questions/11889625/annotating-text-on-individual-facet-in-ggplot2) may help you. Without access to your input data it's difficult to provide a concrete solution. For future reference, you maximise your chance of getting a useful answer if you provide a minimum reproducible example. [This post](https://stackoverflow.com/help/minimal-reproducible-example) may help. – Limey Mar 18 '21 at 08:35
  • Not sure why `scales="free_x"` is not apparently working for you. Do you have y value numbers in your dataset for the dates which are appearing blank? Can you please share your data with us in a reproducible way? To do that, type `dput(Andmed3)` into the console, then copy and paste the wall of code you'll see into the body of your question, formatted as code. The code will look something like `structure(...)`. This way we can reproduce your dataframe and your resulting chart with the code you provided. – chemdork123 Mar 18 '21 at 21:33

0 Answers0