0

I am a newbie here.

1, The aim is to plot a graph about the mean NDVI value during a time period (8 dates were chosen from 2019-05 to 2019-10) of my study site (named RB1). And plot vertical lines to show the date with a grass cutting event.

2, Now I had calculated the NDVI value for these 8 chosen dates and made a CSV file. (PS. the "cutting" means when the grassland on the study site has been cut, so the corresponding dates should be show as a vertical line, using geom_vline)

infor <- read_csv("plotting information.csv")
infor
# A tibble: 142 x 3
   date         NDVI cutting
   <date>      <dbl> <lgl>  
 1 2019-05-12 NA     NA     
 2 2019-05-13 NA     NA     
 3 2019-05-14 NA     NA     
 4 2019-05-15 NA     NA     
 5 2019-05-16 NA     NA     
 6 2019-05-17  0.787 TRUE      
# ... with 132 more rows

3, the problem is, when I do the ggplot, first I want to keep the x-axis as the whole time period (2019-05 to 2019-10) but of course not show all dates in between, otherwise there will be way too many dates show on the x-axis). So, I do the scale_x_discrte(breaks=, labels=) to show the specific dates with NDVI values.

Second I also want to show the dates that the grasses were cut geom_vline.

BUT, it seems like the precondition for scale_x_discrte is to factor my date, while the precondition for geom_vline is to keep the date as nummeric. these two calls seems to be contradictory.

y1 <- ggplot(infor, aes(factor(date), NDVI, group = 1)) +
  geom_point() +
  geom_line(data=infor[!is.na(infor$NDVI),]) + 
  scale_x_discrete(breaks = c("2019-05-17", "2019-06-18", "2019-06-26", "2019-06-28","2019-07-23","2019-07-28", "2019-08-27","2019-08-30", "2019-09-21"), 
                   labels = c("0517","0618","0626","0628","0723","0728", "0827","0830","0921"))) 


y2 <- ggplot(infor, aes(date, NDVI, group = 1)) +
  geom_point() +
  geom_line(data=infor[!is.na(infor$NDVI),])) 

when I add the geom_vline in the y1, vertical lines do not show on my plot: y1 + geom_vline

when I add it in the y2, vertical lines were showed, but the dates (x axis) are weird (not show as the y1 because we donot run the scale_x_ here) y2 + geom_vline

   y1 + 
      geom_vline(data=filter(infor,cutting == "TRUE"), aes(xintercept = as.numeric(date)), color = "red", linetype ="dashed")
  • Hi Chryseis, when your date column is of the class "date", so when you do set it as a factor, the entries you have will be equally spaced. You lose the scale of the time here, i.e the difference between time points. Is this what you want? – StupidWolf Nov 22 '19 at 09:25
  • y2 + geom_vline looks correct to me.. Can you elaborate what's the problem? – StupidWolf Nov 22 '19 at 09:27
  • @StupidWolf thanks for your comment! How's the x-axis looks like at your side? Because for me when I do y2 + geom_vline, it shows the vertical lines, but I lost the specific dates on the x-aixs. (as you could see on the image I put, it shows "Jun"/ "Jul".... ) – Chryseis Ho Nov 22 '19 at 10:17
  • I cannot reproduce your plot as you did not provide the data :) You can dput(infor) and paste the output as part of your post. Good practice so that others can also see. Yes with y2, breaks are in months. You can however do scale_x_continuous and define the breaks manually – StupidWolf Nov 22 '19 at 10:22
  • @StupidWolf I did the "factor()" so that later I can do the "scale_x_discrete" to "break and labels" those chosen specific dates and show them on the x-aixs (as the image of y1 + geom_vline) – Chryseis Ho Nov 22 '19 at 10:23
  • 1
    ah! you are right. let me try to provide some sample data – Chryseis Ho Nov 22 '19 at 10:25

0 Answers0