小言_互联网的博客

利用R绘制指定区域的DEM图

383人阅读  评论(0)

  
  1. # 加载运行环境
  2. library(tidyverse)
  3. library(sf)
  4. library(raster)
  5. library(rgdal)
  6. library(glue)
  7. library(data.table)
  8. library(ggsn)
  9. library(cowplot)
  10. ## read dem file
  11. myfile <- "00DataSet/dem_250m/dem_250m/w001001.adf"
  12. dem <- new( "GDALReadOnlyDataset", myfile) %>% asSGDF_GROD
  13. crs.geo <- CRS( "+proj=longlat +ellps=WGS84 +datum=WGS84")
  14. ## get the boundary of Three River Plain
  15. sjpy <-
  16. read_sf( "00DataSet/Province/CN-sheng-A.shp",
  17. options = "ENCODING=gb2312",
  18. stringsAsFactors = FALSE) %>%
  19. filter(name == "辽宁") %>%
  20. st_transform(crs = crs.geo)
  21. bdf <-sjpy %>% getElement( "geometry") %>%
  22. st_transform(crs = crs.geo) %>% as_Spatial()
  23. ## mask dem using the boundary
  24. dem.sj <- raster(dem)
  25. dem.sj <- dem.sj %>% crop(bdf) %>%
  26. raster::aggregate(fact = 0.01 / res(dem.sj)) %>% #设置分辨率
  27. mask(bdf) %>% as.data.frame(xy = TRUE)
  28. SitesinRegion <- readRDS( "02 DEM/Liaoning/pointsinRegions.rds")
  29. pal8 <- c( "#33A02C", "#B2DF8A", "#FDBF6F", "#1F78B4", "#999999", "#E31A1C", "#E6E6E6", "#A6CEE3")
  30. G1 <- ggplot(dem.sj) + geom_raster(aes(x, y, fill = band1), na.rm = TRUE) +
  31. scale_fill_gradientn(colours = pal8, na.value = "transparent")+
  32. geom_point(data = SitesinRegion,aes(x=X,y=Y,size= "a"))+
  33. scale_size_manual(
  34. values = c(1),
  35. breaks = c( "a"),
  36. labels = c( "Site")
  37. ) +
  38. geom_text(data = SitesinRegion,
  39. aes(x = X, y = Y, label = EnStationN),
  40. size=2.5,check_overlap =TRUE,nudge_y=0.1) +
  41. north(sjpy,location = "topleft" ,0.1)+
  42. ylim(38.3,43.3)+
  43. coord_sf(crs = crs.geo)+
  44. theme(
  45. plot.background=element_rect(fill = "transparent"),
  46. panel.ontop = TRUE,
  47. panel.background = element_rect(color = NA, fill = NA),
  48. panel.grid=element_line(color = "grey",size = 0.25),
  49. panel.border=element_rect(color = "black",fill = NA ),
  50. legend.key = element_rect(fill=NA,size=0.5,color = NA),
  51. legend.title = element_text(size=8),
  52. legend.position = c(1,0),
  53. legend.justification = c(1,0.08),
  54. legend.spacing.y = unit(-0.2, "cm"),
  55. legend.background = element_rect(fill = "transparent"),
  56. plot.margin = margin(0,0,-0.2,-0.3, "cm")
  57. )+
  58. labs(fill= "Elevation(m)",x= "",y= "",size= "")+
  59. guides(size = guide_legend(order=2,title=NULL),
  60. fill=guide_colorbar(order=1,title.vjust=10,barheight =5 ) )
  61. # China mini map ------------------------------------------------------------------------------
  62. China <- read_sf(
  63. "00DataSet/Province/CN-sheng-A.shp",
  64. options = "ENCODING=gb2312",
  65. stringsAsFactors = FALSE
  66. ) %>%
  67. st_transform(crs = crs.geo)
  68. G.China <- ggplot(China)+geom_sf(fill= "white",color= "black",size=0.1)+
  69. geom_sf(data=sjpy,fill= "blue",color= "black",size=0.1)+
  70. coord_sf(crs = crs.geo)+theme_void()
  71. # insert mini China map into the plot ---------------------------------------------------------
  72. Gs <- cowplot::ggdraw()+
  73. cowplot::draw_grob(ggplotGrob(G.China),x = 0,y = 0,
  74. scale = 0.3,
  75. hjust = 0.24, vjust = 0.3)+
  76. draw_plot(G1)
  77. # draw and preview ----------------------------------------------------------------------------
  78. Taotao::preview.ggplot(Gs,width = 12,height = 10,dpi = 600)

 


转载:https://blog.csdn.net/Theo93/article/details/104637647
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场