Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. library(rgdal)
  2. library(rgeos)
  3. library(raster)
  4. library(ggplot2)
  5. library(ggthemes)
  6. library(ggsn)
  7. library(ggmap)
  8. library(maps)
  9. library(maptools)
  10. library(grid)
  11. library(GISTools)
  12. library(celestial)
  13. library(bibtex)
  14. library(plyr) # this needs to be always before dplyr, otherwise, it's masking important functions
  15. library(dplyr)
  16. library(SoDA)
  17.  
  18. par(bg = "white")
  19. puerto.ayora = data.frame(lon = -90.3138000,
  20. lat = -0.7401800,
  21. row.names = "puerto.ayora")
  22. el.garrapatero = data.frame(lon = -90.221744,
  23. lat = -0.687647,
  24. row.names = "el.garrapatero")
  25. locations = rbind(puerto.ayora,
  26. el.garrapatero)
  27. coord.deg = locations
  28. class(coord.deg)
  29. coordinates(coord.deg)<-~lon+lat
  30. class(coord.deg)
  31. proj4string(coord.deg) # nope
  32. proj4string(coord.deg)<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
  33. URL <- "https://osm2.cartodb.com/api/v2/sql?filename=public.galapagos_islands&q=select+*+from+public.galapagos_islands&format=geojson&bounds=&api_key="
  34. fil <- "gal.json"
  35. if (!file.exists(fil)) download.file(URL,
  36. fil)
  37. gal <- readOGR(fil,
  38. "OGRGeoJSON")
  39.  
  40. URL.topo <- "https://osm2.carto.com/api/v2/sql?filename=contour_1&q=select+*+from+public.contour_1&format=geojson&bounds=&api_key="
  41. fil.topo = "public.geojson.json.topo"
  42. if (!file.exists(fil.topo)) download.file(URL.topo,
  43. destfile = fil.topo)
  44. topo <- readOGR("public.geojson.json.topo",
  45. layer ="OGRGeoJSON")
  46. topo <- spTransform(topo,
  47. proj4string(gal)) # epsg:4326 is decimal degree,
  48. # epsg:31983 is meters only,
  49. # 31969 works too
  50. a <- aggregate(gal)
  51. b <- disaggregate(a)
  52. # Setting the theme
  53. theme_set(theme_bw())
  54. #Create a custom color scale
  55. coord.deg<-spTransform(coord.deg,
  56. CRS(proj4string(gal)))
  57. # double check that they match
  58. identical(proj4string(coord.deg),
  59. proj4string(gal)) # look also proj4string(topo)
  60. id=1:length(coord.deg)
  61. my_pts <- SpatialPointsDataFrame(coords = coord.deg,
  62. data=data.frame(id=1:length(coord.deg)))
  63.  
  64. gal@data$id <- seq(gal@data$fid) - 1
  65. gal_union <- unionSpatialPolygons(gal,
  66. IDs = rep(1,length(gal)))
  67. gal_map <- fortify(gal)
  68. gal_ff <- fortify(gal)
  69. gal_union_ff <- fortify(gal_union)
  70.  
  71. topo_map <- fortify(topo)
  72. topo_map <- plyr::join(topo_map,
  73. topo@data, by="id")
  74. names(topo_map)
  75. # ggplot can't deal with a SpatialPointsDataFrame so we
  76. # can convert back to a data.frame
  77. my_pts <- data.frame(my_pts)
  78. my_pts.final = my_pts[ ,2:3]
  79. # we're not dealing with lat/long but with x/y
  80. # this is not necessary but for clarity change variable names
  81. names(my_pts.final)[names(my_pts.final)=="lat"]<-"y"
  82. names(my_pts.final)[names(my_pts.final)=="lon"]<-"x"
  83.  
  84. # Complete Galápagos Island -------------------------------------------
  85.  
  86. ### settings ###
  87.  
  88. ## colour of the map
  89. # ground
  90. ground = "darkseagreen" # "#8FBC8F"
  91. ground.contour = "black"
  92. ground.contour.size = .75 # around the islands
  93.  
  94. # water
  95. water = "lightsteelblue1" # "#CAE1FF" # you can put it white "#FFFFFF" if you want
  96. # water = NA # suggestion: NA => no color
  97.  
  98. # border contour
  99. border.map = "black"
  100. panel.border.size = 1
  101. # GPS points
  102. gps.points.col = c("#FF3030") # "red"
  103. size.of.points = 0.7
  104. # grid colour
  105. inside.grid.c = "blue"
  106. inside.grid.s = 0.1
  107. inside.grid.t = "dotted"
  108.  
  109. # scale colour for topographic maps
  110. topo.low = "#8FBC8F" # "darkseagreen"
  111. topo.high = "darkgreen" # "#006400"
  112.  
  113. #scale bar size & location
  114. dist.in.km = 50 # half of the distance of the bar
  115. location.scale = "bottomleft"
  116.  
  117. # legend position
  118. leg.pos = "right" # if "none" removes all legends
  119.  
  120. ## text
  121. # Title and axis
  122. title.graph = "Îles Galápagos"
  123. y.lab.map = "Latitude"
  124. x.lab.map = "Longitude"
  125. legend.title = "Altitude"
  126. ticks.length = .2
  127. # size (note that the scalebar is different)
  128. text.s = 12
  129. title.mult = 1.5 # multiplicator relative to text.s, so that the title is bigger
  130. # scale
  131. text.scale = text.s/3
  132.  
  133. ## topographic information
  134. # lines
  135. topo.line.size = 0.1
  136. transparent.topo = 0.4
  137. linetype.topo = 1
  138. ### end settings ###
  139.  
  140. gg1 <- ggplot()
  141. gg <- gg1 + geom_path(aes(x = long,
  142. y = lat,
  143. group = group),
  144. data = gal_map,
  145. colour = ground.contour, # will draw each islands
  146. size = ground.contour.size) + # Size!
  147. geom_polygon(data = gal_map,
  148. aes(x = long,
  149. y = lat,
  150. group = group),
  151. color = ground, # colour of the ground
  152. fill = ground) # colour of the space between the polygon. Otherwise, you get weird lines.
  153.  
  154. gg <- gg + ggsn:::scalebar(data = gal_map, # scale bar
  155. dist= dist.in.km, # set the size of 1/2 of the scalebar
  156. location= location.scale,
  157. model = "WGS84", # options ("WGS84", "GRS80", "Airy", "International", "Clarke", "GRS67")
  158. dd2km = TRUE, # this must be true in order to use the distance as km
  159. st.size=text.scale, # number to indicate the scale bar text size. It is passed to the size argument of annotate function.
  160. st.bottom = TRUE) # scale bar text is displayed at the bottom of the scale bar
  161. gg <- gg + #theme_map() + # veryyyyyyyyyyyyyyyyyy sober
  162. theme(plot.title = element_text(family="Helvetica",
  163. size = text.s*title.mult,
  164. face = "bold"),
  165. legend.position = leg.pos,
  166. legend.text = element_text(size = text.s,
  167. colour = "black",
  168. angle = 0),
  169. axis.text = element_text(family="Helvetica",
  170. size=text.s), # text size
  171. axis.title = element_text(family="Helvetica",
  172. size=text.s), # text size
  173. axis.ticks.length = unit(ticks.length,
  174. "cm"), # length of the axis ticks
  175. panel.background = element_rect(fill = water), # white for water
  176. # remove the default grey, you can add blue color for
  177. # water (# background making illusion that this is water)
  178. panel.grid.major = element_blank(), #element_line(colour = inside.grid.c,
  179. # linetype = inside.grid.t,size = inside.grid.s),
  180. # element_blank() if you don't want the grid
  181. legend.background = element_blank(), # remove background from legend
  182. panel.grid.major = element_blank(), # remove grid
  183. panel.grid.minor = element_blank(), # remove grid
  184. strip.background = element_blank(), # remove grid
  185. plot.background = element_blank(), # remove white background of
  186. # the plot (if you want to put your graph in a publication or presentation that has another colour)
  187. panel.border = element_rect(colour = border.map, # border to the map
  188. fill=NA,
  189. size=panel.border.size))
  190. gg <- gg + #geom_polygon(data = subset.c, aes(x = long, y = lat, fill = subset.c[,"id"], group = group)) +
  191. coord_equal()
  192.  
  193. gg <- gg + geom_path(data = topo_map, # adding the elevation line
  194. aes(long,
  195. lat,
  196. group=group, # group by island
  197. colour = elev),
  198. size = topo.line.size , # based on your feeling !
  199. alpha=transparent.topo, # if you want to see through the lines (depth perseption)
  200. linetype = linetype.topo) + # 1 - solid line
  201. scale_color_gradient(low = topo.low, # colour lower elevation
  202. high = topo.high, # colour higher elevation
  203. guide = "colourbar") + # guide = "none" if you don't want
  204. labs(title = title.graph, # title of the graph
  205. y = y.lab.map, # title of y axis
  206. x = x.lab.map, # title of x axis
  207. colour = legend.title) #+
  208. # Adding points only at the end
  209. gg <- gg + geom_point(data=my_pts.final[,], # sites points
  210. aes(x = x,
  211. y = y,
  212. size = size.of.points),
  213. color = gps.points.col) +
  214. geom_text(data=my_pts.final[1,], aes(x = x, y = y,
  215. label=c("Puerto Ayora")),
  216. size = text.scale,
  217. vjust = +0.6, # moves things down with a +
  218. hjust = -0.1) +
  219. geom_text(data=my_pts.final[2,], aes(x = x, y = y,
  220. label=c("El Garrapatero")),
  221. size = text.scale, vjust = +0.2, hjust = -0.1)
  222.  
  223.  
  224. gg <- gg + coord_equal() # to get a nice projection
  225. df <- data.frame(x1 = -89.5, x2 = -89.5, y1 = 1, y2 = 1.25)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement