Advertisement
thiagoveloso

R code to extract point data from the CHIRPS precip dataset

Apr 27th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.77 KB | None | 0 0
  1. library(raster)
  2. library(ncdf4)
  3. library(reshape2)
  4. library(ggplot2)
  5.  
  6. # Open file
  7. b = stack('Downloads/chirps-v2.0.2015.days_p25.nc')
  8.  
  9. # Set coordinates
  10. lon = c(-47.5, -47.5, -44.5)
  11. lat = c(-18.5, -19.5, -20.5)
  12. xy = cbind(lon, lat)
  13.  
  14. # Extract values from point
  15. e = extract(b, xy, df=TRUE)
  16.  
  17. # Fix data frame
  18. df = data.frame(t(e)[-1,])
  19. df = cbind(as.Date(substr(rownames(df), 2, 14), "%Y.%m.%d"), df)
  20. row.names(df) = NULL; names(df) = c('date','location1', 'location2', 'location3')
  21.  
  22. # Take a look
  23. head(df)
  24.  
  25. # Melt
  26. df.m = melt(df, id.vars = 'date')
  27.  
  28. # Plota como linhas
  29. ggplot(df.m) +
  30. geom_line(aes(x = date, y = value)) +
  31. facet_grid(.~variable)
  32.  
  33. # Plota como barras
  34. ggplot(df.m, aes(x = date, y = value)) +
  35. geom_bar(stat = "identity") +
  36. facet_grid(.~variable)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement