Guest User

Untitled

a guest
Oct 22nd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. setwd("D:/Mapping-R/Returns-Practice")
  2.  
  3. library(ggplot2)
  4. library(gstat)
  5. library(sp)
  6. library(maptools)
  7.  
  8. tz <- read.csv("TZ_ReturnPeriods.csv", header = TRUE)
  9.  
  10. temp <- tz
  11. temp$x <- temp$Lon
  12. temp$y <- temp$Lat
  13.  
  14. coordinates(temp) <- ~x + y
  15.  
  16. plot(temp)
  17.  
  18. x.range <- as.numeric(c(28.61, 41.19))
  19. y.range <- as.numeric(c(-12.61, -0.19))
  20.  
  21. tz.grid <- expand.grid(x = seq(from = x.range[1],
  22. to = x.range[2],
  23. by = 0.1),
  24. y = seq(from = y.range[1],
  25. to = y.range[2],
  26. by = 0.1))
  27. coordinates(tz.grid) <- ~x + y
  28.  
  29. plot(tz.grid, cex = 1.5, col = "grey")
  30. points(temp, pch = 16, col = "blue", cex = 0.5)
  31.  
  32. idw <- idw(formula = Return.10 ~ 1,
  33. locations = temp,
  34. newdata = tz.grid)
  35. idw.output <- as.data.frame(idw)
  36. names(idw.output)[1:3] <- c("Longitude", "Latitude", "Return")
  37.  
  38. ggplot() + geom_tile(data = idw.output,
  39. aes(x = Longitude,
  40. y = Latitude,
  41. fill = Return)) +
  42. geom_point(data = tz,
  43. aes(x = Lon, y = Lat),
  44. shape = 21,
  45. color = "red")
  46.  
  47. tz.contour <- readShapePoly("TZA_adm0.shp")
  48. tz.contour <- fortify(tz.contour, region = "NAME_0")
  49.  
  50. ggplot() + geom_tile(data = idw.output,
  51. alpha = 0.8,
  52. aes(x = Longitude,
  53. y = Latitude,
  54. fill = round(Return, 0))) +
  55. scale_fill_gradient(low = "cyan", high = "orange") +
  56. geom_path(data = tz.contour, aes(long, lat, group = group), color = "grey") +
  57. geom_point(data = tz, aes(x = Lon, y = Lat), shape = 16, cex = 0.5, color = "red") +
  58. labs(fill = "Rainfall", title = "10 Year Return Period in Tanzania")
  59.  
  60. tz.contour <- readShapePoly("TZA_adm0.shp") # use this to clip
  61. tz.contour.dfr <- fortify(tz.contour, region = "NAME_0") # use this to plot
  62.  
  63. library(raster)
  64. idw.r <- rasterFromXYZ(idw.output[, c("Longitude", "Latitude", "Return")])
  65. idw.crp <- crop(idw.r, tz.contour)
  66. idw.msk <- mask(idw.crp, tz.contour)
  67. idw.msk.dfr <- as.data.frame(rasterToPoints(idw.msk))
  68. names(idw.msk.dfr)[1:2] <- c("Longitude", "Latitude")
  69.  
  70. # now plot:
  71. ggplot() + geom_tile(data = idw.msk.dfr,
  72. alpha = 0.8,
  73. aes(x = Longitude,
  74. y = Latitude,
  75. fill = round(Return, 0))) +
  76. scale_fill_gradient(low = "cyan", high = "orange") +
  77. geom_path(data = tz.contour.dfr, aes(long, lat, group = group), color = "grey") +
  78. geom_point(data = tz, aes(x = Lon, y = Lat), shape = 16, cex = 0.5, color = "red") +
  79. labs(fill = "Rainfall", title = "10 Year Return Period in Tanzania") +
  80. coord_equal() +
  81. theme(panel.grid.major = element_blank(),
  82. panel.grid.minor = element_blank(),
  83. panel.background = element_blank())
  84.  
  85. data <- RwandanDataset
  86. data$x <- data$longitude
  87. data$y <- data$latitude
  88.  
  89. coordinates(data) <- ~x + y
  90.  
  91. plot(data)
  92.  
  93. x.min <- min(RwandanDataset$longitude)
  94. x.max <- max(RwandanDataset$longitude)
  95.  
  96. y.min <- min(RwandanDataset$latitude)
  97. y.max <- max(RwandanDataset$latitude)
  98.  
  99. tz.grid <- expand.grid(x = seq(from = x.min,
  100. to = x.max,
  101. by = 0.1),
  102. y = seq(from = y.min,
  103. to = y.max,
  104. by = 0.1))
  105.  
  106. coordinates(tz.grid) <- ~x + y
  107.  
  108. plot(tz.grid, cex = 1.5, col = "grey")
  109. points(data, pch = 16, col = "blue", cex = 0.5)
  110. library(gstat)
  111. idw <- idw(formula = elevation ~ 1,
  112. locations = data,
  113. newdata = tz.grid)
  114. idw.output <- as.data.frame(idw)
  115. names(idw.output)[1:3] <- c("Longitude", "Latitude", "Return")
  116.  
  117. ggplot() + geom_tile(data = idw.output,
  118. aes(x = Longitude,
  119. y = Latitude,
  120. fill = Return)) +
  121. geom_point(data = RwandanDataset,
  122. aes(x = longitude, y = latitude),
  123. shape = 21,
  124. color = "red")
  125. tz.contour<- readOGR("Rwanda_adm1.shp")
  126. tz.contour.dfr <- fortify(tz.contour,group="group")
  127.  
  128. library(raster)
  129. idw.r <- rasterFromXYZ(idw.output[, c("Longitude", "Latitude", "Return")])
  130. idw.crp <- crop(idw.r, tz.contour)
  131. idw.msk <- mask(idw.crp, tz.contour)
  132. idw.msk.dfr <- as.data.frame(rasterToPoints(idw.msk))
  133. names(idw.msk.dfr)[1:2] <- c("Longitude", "Latitude")
  134.  
  135. # plot:
  136. ggplot() + geom_tile(data = idw.msk.dfr,
  137. alpha = 0.8,
  138. aes(x = Longitude,
  139. y = Latitude,
  140. fill = round(Return, 0))) +
  141. scale_fill_gradient(low = "cyan", high = "orange") +
  142. geom_path(data = tz.contour.dfr, aes(long, lat, group = group), color = "grey") +
  143. geom_point(data = RwandanDataset,aes(x = longitude, y = latitude), shape = 16, cex = 0.5, color = "red") +
  144. labs(fill = "Rainfall", title = "Elevation") +
  145. coord_equal() +
  146. theme(panel.grid.major = element_blank(),
  147. panel.grid.minor = element_blank(),
  148. panel.background = element_blank())
Add Comment
Please, Sign In to add comment