R – ggplot 在国际国家上映射数据

huangapple go评论117阅读模式
英文:

R - ggplot mapping data on international country

问题

我尝试在坦桑尼亚的Mwanihana森林中绘制一个缩放视图。

我已经查看了在R中绘制纬度和经度点,但无法使其适用于我的数据集。

数据如下:

  1. date forestsite primaryinterface secondaryinterface
  2. <chr> <chr> <chr> <chr>
  3. 8.27.13 UMNP-HQ ecotourism forest use/PD
  4. 8.28.13 UMNP-campsite3 ecotourism forest use
  5. 8.28.13 UMNP-campsite3 ecotourism forest use
  6. 8.28.13 UMNP-hondohondoa ecotourism forest use/PD
  7. 8.28.13 UMNP-hondohondob ecotourism forest use/PD/crop-raiding
  8. 8.29.13 UMNP-njokamoni ecotourism forest use
  9. 8.29.13 UMNP-mangabey ecotourism forest use
  10. anthrochange species geometry
  11. <chr> <chr> <S3: sfc_POINT>
  12. small trails, snares BWC <S3: sfc_POINT>
  13. roads, buildings RC <S3: sfc_POINT>
  14. roads, campsite RC <S3: sfc_POINT>
  15. roads, campsite BWC <S3: sfc_POINT>
  16. none SY <S3: sfc_POINT>
  17. clearcut road, houses, crops YB <S3: sfc_POINT>
  18. trail to njokamoni RC <S3: sfc_POINT>
  19. small trails, habituated troop SM <S3: sfc_POINT>

地图数据和ggplot尝试如下:

  1. library(elevatr)
  2. library(rgdal)
  3. se.elevations <- get_elev_raster(locations = world, z = 7, clip = "locations")
  4. se.elevation.df <- raster::as.data.frame(se.elevations, xy = TRUE) %>%
  5. rename(elevation = 3) %>%
  6. na.omit() %>%
  7. filter(elevation >= 0)
  8. xlimit <- c(36.85, 37)
  9. ylimit <- c(-7.76, -7.88)
  10. ggplot() +
  11. geom_tile(data = se.elevation.df, aes(x = x, y = y, fill = elevation)) +
  12. scale_fill_gradientn(colours= c("gray", "black")) +
  13. geom_sf(data = world, fill = NA) +
  14. xlim(xlimit) +
  15. ylim(ylimit) +
  16. geom_sf(data = data, aes(color = forestsite, shape = primaryinterface), size = 3) +
  17. coord_sf(xlim = c(36.85, 37), ylim = c(-7.76, -7.88), expand = FALSE)

我能够得到这张地图:

从以下代码:

  1. ggplot() +
  2. geom_sf(data = data, aes(color = species, shape = primaryinterface), size = 3) +
  3. coord_sf(xlim = c(36.85, 37), ylim = c(-7.76, -7.88), expand = FALSE) -> ggplottt

但每当我尝试添加我的地图数据以包括坦桑尼亚的纬度和经度时,我会收到各种错误消息。

任何建议都将不胜感激。

Deput

英文:

I am trying to map a zoomed-in view along the Mwanihana forest in Tanzania.

I have looked into Plotting latitude and longitude points in r, but can't get it to work for my dataset.

Data <-

  1. date forestsite primaryinterface secondaryinterface
  2. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  3. 8.27.13 UMNP-HQ ecotourism forest use/PD
  4. 8.28.13 UMNP-campsite3 ecotourism forest use
  5. 8.28.13 UMNP-campsite3 ecotourism forest use
  6. 8.28.13 UMNP-hondohondoa ecotourism forest use/PD
  7. 8.28.13 UMNP-hondohondob ecotourism forest use/PD/crop-raiding
  8. 8.29.13 UMNP-njokamoni ecotourism forest use
  9. 8.29.13 UMNP-mangabey ecotourism forest use
  10. anthrochange species geometry
  11. &lt;chr&gt; &lt;chr&gt; &lt;S3: sfc_POINT&gt;
  12. small trails, snares BWC &lt;S3: sfc_POINT&gt;
  13. roads, buildings RC &lt;S3: sfc_POINT&gt;
  14. roads, campsite RC &lt;S3: sfc_POINT&gt;
  15. roads, campsite BWC &lt;S3: sfc_POINT&gt;
  16. none SY &lt;S3: sfc_POINT&gt;
  17. clearcut road, houses, crops YB &lt;S3: sfc_POINT&gt;
  18. trail to njokamoni RC &lt;S3: sfc_POINT&gt;
  19. small trails, habituated troop SM &lt;S3: sfc_POINT&gt;

Map data and ggplot attempt

  1. library(elevatr)
  2. library(rgdal)
  3. se.elevations &lt;- get_elev_raster(locations = world, z = 7, clip = &quot;locations&quot;)
  4. se.elevation.df &lt;- raster::as.data.frame(se.elevations, xy = TRUE) %&gt;%
  5. rename(elevation = 3) %&gt;%
  6. na.omit() %&gt;%
  7. filter(elevation &gt;= 0)
  8. xlimit &lt;- c(36.85, 37)
  9. ylimit &lt;- c(-7.76, -7.88)
  10. ggplot()+
  11. geom_tile(data = se.elevation.df, aes(x = x, y = y, fill = elevation))+
  12. scale_fill_gradientn(colours= c(&quot;gray&quot;, &quot;black&quot;))+
  13. geom_sf(data = world, fill = NA)+
  14. xlim(xlimit)+
  15. ylim(ylimit)+
  16. geom_sf(data = data, aes(color = forestsite, shape = primaryinterface), size = 3) +
  17. coord_sf(xlim = c(36.85, 37), ylim = c(-7.76, -7.88), expand = FALSE)

I am able to get this map -

from this code

  1. ggplot() +
  2. geom_sf(data = data, aes(color = species, shape = primaryinterface), size = 3) +
  3. coord_sf(xlim = c(36.85, 37), ylim = c(-7.76, -7.88), expand = FALSE) -&gt; ggplottt

R – ggplot 在国际国家上映射数据

but anytime I try to add the mymap data to include the Tanzanian lat, long, I get various error messages.

Any advice is greatly appreciated.

Deput

  1. structure(list(date = c(&quot;8.27.13&quot;, &quot;8.28.13&quot;, &quot;8.28.13&quot;, &quot;8.28.13&quot;,
  2. &quot;8.28.13&quot;, &quot;8.29.13&quot;, &quot;8.29.13&quot;), forestsite = c(&quot;UMNP-HQ&quot;, &quot;UMNP-campsite3&quot;,
  3. &quot;UMNP-campsite3&quot;, &quot;UMNP-hondohondoa&quot;, &quot;UMNP-hondohondob&quot;, &quot;UMNP-njokamoni&quot;,
  4. &quot;UMNP-mangabey&quot;), primaryinterface = c(&quot;ecotourism&quot;, &quot;ecotourism&quot;,
  5. &quot;ecotourism&quot;, &quot;ecotourism&quot;, &quot;ecotourism&quot;, &quot;ecotourism&quot;, &quot;ecotourism&quot;
  6. ), secondaryinterface = c(&quot;forest use/PD&quot;, &quot;forest use&quot;, &quot;forest use&quot;,
  7. &quot;forest use/PD&quot;, &quot;forest use/PD/crop-raiding&quot;, &quot;forest use&quot;,
  8. &quot;forest use&quot;), anthrochange = c(&quot;roads, buildings&quot;, &quot;roads, campsite&quot;,
  9. &quot;roads, campsite&quot;, &quot;none&quot;, &quot;clearcut road, houses, crops&quot;, &quot;trail to njokamoni&quot;,
  10. &quot;small trails, habituated troop&quot;), species = c(&quot;RC&quot;, &quot;RC&quot;, &quot;BWC&quot;,
  11. &quot;SY&quot;, &quot;YB&quot;, &quot;RC&quot;, &quot;SM&quot;), geometry = structure(list(structure(c(36.883988,
  12. -7.844929), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.884545,
  13. -7.849439), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.884545,
  14. -7.849439), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.887065,
  15. -7.833501), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.891369,
  16. -7.832091), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.878388,
  17. -7.82738), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.87753,
  18. -7.8232), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;))), class = c(&quot;sfc_POINT&quot;,
  19. &quot;sfc&quot;), precision = 0, bbox = structure(c(xmin = 36.87753, ymin = -7.849439,
  20. xmax = 36.891369, ymax = -7.8232), class = &quot;bbox&quot;), crs = structure(list(
  21. input = &quot;EPSG:4326&quot;, wkt = &quot;GEOGCRS[\&quot;WGS 84\&quot;,\n ENSEMBLE[\&quot;World Geodetic System 1984 ensemble\&quot;,\n MEMBER[\&quot;World Geodetic System 1984 (Transit)\&quot;],\n MEMBER[\&quot;World Geodetic System 1984 (G730)\&quot;],\n MEMBER[\&quot;World Geodetic System 1984 (G873)\&quot;],\n MEMBER[\&quot;World Geodetic System 1984 (G1150)\&quot;],\n MEMBER[\&quot;World Geodetic System 1984 (G1674)\&quot;],\n MEMBER[\&quot;World Geodetic System 1984 (G1762)\&quot;],\n MEMBER[\&quot;World Geodetic System 1984 (G2139)\&quot;],\n ELLIPSOID[\&quot;WGS 84\&quot;,6378137,298.257223563,\n LENGTHUNIT[\&quot;metre\&quot;,1]],\n ENSEMBLEACCURACY[2.0]],\n PRIMEM[\&quot;Greenwich\&quot;,0,\n ANGLEUNIT[\&quot;degree\&quot;,0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\&quot;geodetic latitude (Lat)\&quot;,north,\n ORDER[1],\n ANGLEUNIT[\&quot;degree\&quot;,0.0174532925199433]],\n AXIS[\&quot;geodetic longitude (Lon)\&quot;,east,\n ORDER[2],\n ANGLEUNIT[\&quot;degree\&quot;,0.0174532925199433]],\n USAGE[\n SCOPE[\&quot;Horizontal component of 3D system.\&quot;],\n AREA[\&quot;World.\&quot;],\n BBOX[-90,-180,90,180]],\n ID[\&quot;EPSG\&quot;,4326]]&quot;), class = &quot;crs&quot;), n_empty = 0L)), row.names = c(NA,
  22. -7L), sf_column = &quot;geometry&quot;, agr = structure(c(date = NA_integer_,
  23. forestsite = NA_integer_, primaryinterface = NA_integer_, secondaryinterface = NA_integer_,
  24. anthrochange = NA_integer_, species = NA_integer_), .Label = c(&quot;constant&quot;,
  25. &quot;aggregate&quot;, &quot;identity&quot;), class = &quot;factor&quot;), class = c(&quot;sf&quot;,
  26. &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))

答案1

得分: 2

我认为最好使用交互地图。数据是df。你需要以良好的格式获取坐标,因为你的对象是sfc_POINT

我与你一起使用dput()

  1. library(sf)
  2. library(leaflet)
  3. library(raster)
  4. library(sp)
  5. # 提取坐标
  6. df$latitudes <- st_coordinates(df$geometry)[, 2]
  7. df$longitudes <- st_coordinates(df$geometry)[, 1]
  8. adm <- getData('GADM', country='TZA', level=1)
  9. popup <- paste0("<strong>人口: </strong>",
  10. adm$NAME_1)
  11. # 坦桑尼亚交互地图
  12. TZA_MAP <- leaflet() %>%
  13. addTiles() %>%
  14. addPolygons(data=adm, weight = 2, col = 'blue', fillColor = "#FEF800", popup=popup,
  15. fillOpacity = 0.1) |
  16. addCircles(data = df ,lat= df$latitudes , lng= df$longitudes,
  17. label = c(df$primaryinterface, df$secondaryinterface), labelOptions =
  18. labelOptions(noHide = T, direction = "bottom"),
  19. group = c(df$primaryinterface, df$secondaryinterface)) |
  20. addLayersControl(overlayGroup= c(df$primaryinterface, df$secondaryinterface),
  21. options = layersControlOptions(collapsed=F))
  22. TZA_MAP

你可以选择primaryinterfacesecondaryinterface来进行筛选,但你可以在函数中更改你想要做的操作。

英文:

I think that is better an interactive map. The data is df. You need get the coordinate in a good format because your object is a sfc_POINT.

I'm work with you dput()

  1. library(sf)
  2. library(leaflet)
  3. library(raster)
  4. library(sp)
  5. # Extract coordinates
  6. df$latitudes &lt;- st_coordinates(df$geometry)[, 2]
  7. df$longitudes &lt;- st_coordinates(df$geometry)[, 1]
  8. adm &lt;- getData(&#39;GADM&#39;, country=&#39;TZA&#39;, level=1)
  9. popup &lt;- paste0(&quot;&lt;strong&gt;POPULATION: &lt;/strong&gt;&quot;,
  10. adm$NAME_1)
  11. # Tanzania interactive map
  12. TZA_MAP &lt;- leaflet() %&gt;%
  13. addTiles() %&gt;%
  14. addPolygons(data=adm, weight = 2, col = &#39;blue&#39;, fillColor = &quot;#FEF800&quot;, popup=popup,
  15. fillOpacity = 0.1) |&gt;
  16. addCircles(data = df ,lat= df$latitudes , lng= df$longitudes,
  17. label = c(df$primaryinterface, df$secondaryinterface), labelOptions =
  18. labelOptions(noHide = T, direction = &quot;bottom&quot;),
  19. group = c(df$primaryinterface, df$secondaryinterface)) |&gt;
  20. addLayersControl(overlayGroup= c(df$primaryinterface, df$secondaryinterface),
  21. options = layersControlOptions(collapsed=F))
  22. TZA_MAP

R – ggplot 在国际国家上映射数据

You could select for filter primaryinterface and secondaryinterface, but you can change in function what you want to do.

huangapple
  • 本文由 发表于 2023年3月1日 09:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定