当前位置:   article > 正文

Rstudio学习|用已有的底图填充数据绘制地图_底图在绘制地图

底图在绘制地图

1.矢量栅格数据常用两个包

2.设置坐标系

3.导入设置好的底图

  1. library(tidyverse)
  2. library(sf)
  3. # 中国地图通常使用这样的坐标系
  4. mycrs <- "+proj=aea +lat_0=0 +lon_0=105 +lat_1=25 +lat_2=47 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
  5. # 1. 带小地图版本
  6. # 读取小地图版本的中国省级地图
  7. read_sf("E:/R_codedata/SO2与机器人安装密度/SO2与机器人安装密度/chinaprov2019mini/chinaprov2019mini.shp") %>%
  8. filter(!is.na(省代码)) -> provmap
  9. # 线条
  10. read_sf("E:/R_codedata/SO2与机器人安装密度/SO2与机器人安装密度/chinaprov2019mini/chinaprov2019mini_line.shp") %>%
  11. filter(class %in% c("九段线", "海岸线", "小地图框格")) %>%
  12. select(class) -> provlinemap

4.读入数据

  1. readxl::read_xlsx("画图.xlsx") %>%
  2. gather(2:15, key = "key", value = "value") %>% #横坐标轴数据(年份)
  3. mutate(key = str_remove_all(key, "jq")) %>% #去掉关键词中的jq
  4. rename(year = key) %>% #将关键词命名为年份
  5. mutate(prov = str_sub(city, 1, 2)) %>% #Create, modify, and delete columns列
  6. type_convert() -> provdf1

(1)gather() #Gather columns into key-value pairs,将xlsx中的列读取进来

data

A data frame.

  1. gather(
  2. data,
  3. key = "key",
  4. value = "value",
  5. ...,
  6. na.rm = FALSE,
  7. convert = FALSE,
  8. factor_key = FALSE
  9. )

5.填充数据

  1. # 机器人安装密度
  2. readxl::read_xlsx("画图.xlsx") %>%
  3. gather(2:15, key = "key", value = "value") %>% #横坐标轴数据(年份)
  4. mutate(key = str_remove_all(key, "jq")) %>% #去掉关键词中的jq
  5. rename(year = key) %>% #将关键词命名为年份
  6. mutate(prov = str_sub(city, 1, 2)) %>% #Create, modify, and delete columns列
  7. type_convert() -> provdf1
  8. provmap %>%
  9. mutate(prov = str_sub(, 1, 2)) %>%
  10. left_join(provdf1) %>%
  11. mutate(value = if_else(is.na(value), -1, value)) -> provmap2
  12. #对应
  13. # quantile(provmap2$value, probs = 1:8/8, digits = 2) -> cutlist#分位数,通用函数分位数产生对应于给定概率的样本分位数。最小的观察对应于概率0,最大的对应于概率1
  14. # cutlist <- c(-1, 0, cutlist)# c,Combine Values into a Vector or List
  15. # cutlist

 left_join()     |   keeps all observations in x.

The three outer joins keep observations that appear in at least one of the data frames:

  • left_join() keeps all observations in x.

  • right_join() keeps all observations in y.

  • full_join() keeps all observations in x and y.

  1. inner_join(
  2. x,
  3. y,
  4. by = NULL,
  5. copy = FALSE,
  6. suffix = c(".x", ".y"),
  7. ...,
  8. keep = NULL
  9. )
  10. ## S3 method for class 'data.frame'
  11. inner_join(
  12. x,
  13. y,
  14. by = NULL,
  15. copy = FALSE,
  16. suffix = c(".x", ".y"),
  17. ...,
  18. keep = NULL,
  19. na_matches = c("na", "never"),
  20. multiple = "all",
  21. unmatched = "drop",
  22. relationship = NULL
  23. )
  24. left_join(
  25. x,
  26. y,
  27. by = NULL,
  28. copy = FALSE,
  29. suffix = c(".x", ".y"),
  30. ...,
  31. keep = NULL
  32. )
  33. ## S3 method for class 'data.frame'
  34. left_join(
  35. x,
  36. y,
  37. by = NULL,
  38. copy = FALSE,
  39. suffix = c(".x", ".y"),
  40. ...,
  41. keep = NULL,
  42. na_matches = c("na", "never"),
  43. multiple = "all",
  44. unmatched = "drop",
  45. relationship = NULL
  46. )
  47. right_join(
  48. x,
  49. y,
  50. by = NULL,
  51. copy = FALSE,
  52. suffix = c(".x", ".y"),
  53. ...,
  54. keep = NULL
  55. )
  56. ## S3 method for class 'data.frame'
  57. right_join(
  58. x,
  59. y,
  60. by = NULL,
  61. copy = FALSE,
  62. suffix = c(".x", ".y"),
  63. ...,
  64. keep = NULL,
  65. na_matches = c("na", "never"),
  66. multiple = "all",
  67. unmatched = "drop",
  68. relationship = NULL
  69. )
  70. full_join(
  71. x,
  72. y,
  73. by = NULL,
  74. copy = FALSE,
  75. suffix = c(".x", ".y"),
  76. ...,
  77. keep = NULL
  78. )
  79. ## S3 method for class 'data.frame'
  80. full_join(
  81. x,
  82. y,
  83. by = NULL,
  84. copy = FALSE,
  85. suffix = c(".x", ".y"),
  86. ...,
  87. keep = NULL,
  88. na_matches = c("na", "never"),
  89. multiple = "all",
  90. relationship = NULL
  91. )

6.分组分段(图例)

  1. # 分组
  2. provmap2 %>%
  3. mutate(group = cut(value, breaks = cutlist,
  4. include.lowest = T,
  5. labels = c("No data", "<= 186", "186~402", "402~765",
  6. "765~1588", "1588~3009", "3009~6230",
  7. "6230~13740", "> 13740"))) -> provmap2
  8. #去除多余名称
  9. provmap %>%
  10. mutate(label = str_remove_all(, "回族|省|维吾尔自治区|壮族自治区|自治区|特别行政区|市|中朝共有")) -> provmap

 7.绘图|比例尺、图例、指北针、经纬网、边框、字体等

  1. # 绘图
  2. library(ggspatial)
  3. library(ggnewscale)
  4. provmap2 %>%
  5. filter(year == 2006 | is.na(year)) %>%
  6. ggplot() +
  7. geom_sf(aes(fill = group), color = "gray", linewidth = 0.01) +
  8. geom_sf(data = provlinemap,
  9. aes(color = class, linewidth = class),
  10. show.legend = F) +
  11. scale_fill_manual(values = c("gray", "#e0f2f1", "#b2dfdb", "#80cbc4",
  12. "#4db6ac", "#26a69a", "#009688",
  13. "#00897b", "#00796b")) +
  14. stat_sf_coordinates(data = provmap,
  15. geom = "text", color = "#90a4ae",
  16. aes(label = label), family = cnfont,
  17. fun.geometry = st_point_on_surface,
  18. size = 3) +
  19. scale_color_manual(
  20. values = c("九段线" = "#A29AC4",
  21. "海岸线" = "#0055AA",
  22. "小地图框格" = "black")
  23. ) +
  24. scale_linewidth_manual(
  25. values = c("九段线" = 0.6,
  26. "海岸线" = 0.3,
  27. "小地图框格" = 0.3)
  28. ) +
  29. annotation_scale(location = "bl",
  30. width_hint = 0.3,
  31. text_family = cnfont) +
  32. theme(axis.title.x = element_blank(),
  33. axis.title.y = element_blank(),
  34. panel.grid.major = element_blank(),
  35. panel.grid.minor = element_blank(),
  36. axis.text.x = element_blank(),
  37. axis.text.y = element_blank(),
  38. legend.position = c(0.1, 0.2),
  39. legend.title = element_blank(),
  40. plot.background = element_rect(fill = "white", color = "white")) +
  41. annotation_north_arrow(
  42. location = "tr",
  43. which_north = "false",
  44. pad_y = unit(0.1, "cm"),
  45. style = north_arrow_nautical(
  46. text_family = cnfont
  47. )
  48. ) -> p1
  49. ggsave("pic1.png", width = 9, height = 8, device = png)

8.循环|批量制图

  1. # 循环所有年份的
  2. dir.create("robotpic")
  3. for (y in 2006:2019) {
  4. provmap2 %>%
  5. filter(year == y | is.na(year)) %>%
  6. ggplot() +
  7. geom_sf(aes(fill = group), color = "gray", linewidth = 0.01) +
  8. geom_sf(data = provlinemap,
  9. aes(color = class, linewidth = class),
  10. show.legend = F) +
  11. scale_fill_manual(values = c("gray", "#e0f2f1", "#b2dfdb", "#80cbc4",
  12. "#4db6ac", "#26a69a", "#009688",
  13. "#00897b", "#00796b")) +
  14. stat_sf_coordinates(data = provmap,
  15. geom = "text", color = "#90a4ae",
  16. aes(label = label), family = cnfont,
  17. fun.geometry = st_point_on_surface,
  18. size = 3) +
  19. scale_color_manual(
  20. values = c("九段线" = "#A29AC4",
  21. "海岸线" = "#0055AA",
  22. "小地图框格" = "black")
  23. ) +
  24. scale_linewidth_manual(
  25. values = c("九段线" = 0.6,
  26. "海岸线" = 0.3,
  27. "小地图框格" = 0.3)
  28. ) +
  29. annotation_scale(location = "bl",
  30. width_hint = 0.3,
  31. text_family = cnfont) +
  32. guides(fill = guide_legend(ncol = 2, byrow = F)) +
  33. theme(axis.title.x = element_blank(),
  34. axis.title.y = element_blank(),
  35. panel.grid.major = element_blank(),
  36. panel.grid.minor = element_blank(),
  37. axis.text.x = element_blank(),
  38. axis.text.y = element_blank(),
  39. legend.position = c(0.135, 0.2),
  40. legend.title = element_blank(),
  41. plot.background = element_rect(fill = "white", color = "white")) +
  42. annotation_north_arrow(
  43. location = "tr",
  44. which_north = "false",
  45. pad_y = unit(0.1, "cm"),
  46. style = north_arrow_nautical(
  47. text_family = cnfont
  48. )
  49. ) -> ptemp
  50. ggsave(paste0("robotpic/pic", y, ".png"), width = 9, height = 7, device = png)
  51. ggsave(paste0("robotpic/pic", y, ".pdf"), width = 9, height = 7, device = cairo_pdf)
  52. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/270640
推荐阅读
相关标签
  

闽ICP备14008679号