Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

geom_vline doesn't work after the scale_x_discrete in R

$
0
0

I am a newie here, sorry for not writing the question right :p

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

2, Now I have the sentinel2 raster data (Red and NIR band) downloaded from ESA, and have extracted them based on the study site polygon:

c_extract <- function(x, y=RB1_shp){raster::extract(x,y, fun = mean)}

3, then I do the NDVI calculation for each date:

c_DNVI <- function(x,y){(x-y)/(x+y)}

4, so then I made a CSV file according to the results I got from above: (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

5, the problem is, when I do the ggplot, first I want to show the specific date with an NDVI value (scale_x_discrte(breaks=, labels=)); second I want to show the dates that the grasses were cut (geom_vline). BUT, these two calls just cannot work together. I am confused.

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")) +
  labs(x="Dates", y="NDVI", title = "Calculated NDVI of Rottenbuch1 study site in 2019") +
  theme(panel.background=element_blank(),
        axis.line.x=element_line(color="black"),
        axis.line.y=element_line(color="black"),
        axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5)) 


y2 <- ggplot(infor, aes(date, NDVI, group = 1)) +
  geom_point() +
  geom_line(data=infor[!is.na(infor$NDVI),]) + 
  labs(x="Dates", y="NDVI", title = "Calculated NDVI of Rottenbuch1 study site in 2019") +
  theme(panel.background=element_blank(),
        axis.line.x=element_line(color="black"),
        axis.line.y=element_line(color="black"),
        axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5)) 

when I add the geom_vline in the y1, nothing change on my plot; 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_ in y2)

   y1 + 
      geom_vline(data=filter(infor,cutting == "TRUE"), aes(xintercept = as.numeric(date)), color = "red", linetype ="dashed")

Would be appreciated if you can help! thanks in advance! :D


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>