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

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

R - ggplot mapping data on international country

问题

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

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

数据如下:

date     forestsite        primaryinterface secondaryinterface                   
<chr>    <chr>             <chr>            <chr>
8.27.13  UMNP-HQ	       ecotourism	    forest use/PD
8.28.13  UMNP-campsite3	   ecotourism	    forest use
8.28.13  UMNP-campsite3	   ecotourism	    forest use
8.28.13  UMNP-hondohondoa  ecotourism	    forest use/PD
8.28.13	 UMNP-hondohondob  ecotourism       forest use/PD/crop-raiding
8.29.13	 UMNP-njokamoni	   ecotourism	    forest use
8.29.13	 UMNP-mangabey	   ecotourism	    forest use   

anthrochange                     species       geometry
<chr>                            <chr>         <S3: sfc_POINT>      
small trails, snares	         BWC           <S3: sfc_POINT>
roads, buildings	             RC            <S3: sfc_POINT>
roads, campsite	                 RC            <S3: sfc_POINT>
roads, campsite	                 BWC           <S3: sfc_POINT>
none	                         SY            <S3: sfc_POINT>
clearcut road, houses, crops     YB            <S3: sfc_POINT>
trail to njokamoni	             RC            <S3: sfc_POINT>
small trails, habituated troop	 SM            <S3: sfc_POINT>

地图数据和ggplot尝试如下:

library(elevatr)
library(rgdal)

se.elevations <- get_elev_raster(locations = world, z = 7, clip = "locations") 
se.elevation.df <- raster::as.data.frame(se.elevations, xy = TRUE) %>%
  rename(elevation =  3) %>%
  na.omit() %>%
  filter(elevation >= 0)
xlimit <- c(36.85, 37)
ylimit <- c(-7.76, -7.88)

ggplot() +
  geom_tile(data = se.elevation.df, aes(x = x, y = y, fill = elevation)) +
  scale_fill_gradientn(colours= c("gray", "black")) +
  geom_sf(data = world, fill = NA) +
  xlim(xlimit) +
  ylim(ylimit) +
  geom_sf(data = data, aes(color = forestsite, shape = primaryinterface), size = 3) +
    coord_sf(xlim = c(36.85, 37), ylim = c(-7.76, -7.88), expand = FALSE)

我能够得到这张地图:

从以下代码:

ggplot() +
  geom_sf(data = data, aes(color = species, shape = primaryinterface), size = 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 <-

date     forestsite        primaryinterface secondaryinterface                   
&lt;chr&gt;    &lt;chr&gt;             &lt;chr&gt;            &lt;chr&gt;
8.27.13  UMNP-HQ	       ecotourism	    forest use/PD
8.28.13  UMNP-campsite3	   ecotourism	    forest use
8.28.13  UMNP-campsite3	   ecotourism	    forest use
8.28.13  UMNP-hondohondoa  ecotourism	    forest use/PD
8.28.13	 UMNP-hondohondob  ecotourism       forest use/PD/crop-raiding
8.29.13	 UMNP-njokamoni	   ecotourism	    forest use
8.29.13	 UMNP-mangabey	   ecotourism	    forest use   

anthrochange                     species       geometry
&lt;chr&gt;                            &lt;chr&gt;         &lt;S3: sfc_POINT&gt;      
small trails, snares	         BWC           &lt;S3: sfc_POINT&gt;
roads, buildings	             RC            &lt;S3: sfc_POINT&gt;
roads, campsite	                 RC            &lt;S3: sfc_POINT&gt;
roads, campsite	                 BWC           &lt;S3: sfc_POINT&gt;
none	                         SY            &lt;S3: sfc_POINT&gt;
clearcut road, houses, crops     YB            &lt;S3: sfc_POINT&gt;
trail to njokamoni	             RC            &lt;S3: sfc_POINT&gt;
small trails, habituated troop	 SM            &lt;S3: sfc_POINT&gt;

Map data and ggplot attempt

library(elevatr)
library(rgdal)

se.elevations &lt;- get_elev_raster(locations = world, z = 7, clip = &quot;locations&quot;) 
se.elevation.df &lt;- raster::as.data.frame(se.elevations, xy = TRUE) %&gt;%
  rename(elevation =  3) %&gt;%
  na.omit() %&gt;%
  filter(elevation &gt;= 0)
xlimit &lt;- c(36.85, 37)
ylimit &lt;- c(-7.76, -7.88)

ggplot()+
  geom_tile(data = se.elevation.df, aes(x = x, y = y, fill = elevation))+
  scale_fill_gradientn(colours= c(&quot;gray&quot;, &quot;black&quot;))+
  geom_sf(data = world, fill = NA)+
  xlim(xlimit)+
  ylim(ylimit)+
  geom_sf(data = data, aes(color = forestsite, shape = primaryinterface), size = 3) +
    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

ggplot() +
  geom_sf(data = data, aes(color = species, shape = primaryinterface), size = 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

structure(list(date = c(&quot;8.27.13&quot;, &quot;8.28.13&quot;, &quot;8.28.13&quot;, &quot;8.28.13&quot;, 
&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;, 
&quot;UMNP-campsite3&quot;, &quot;UMNP-hondohondoa&quot;, &quot;UMNP-hondohondob&quot;, &quot;UMNP-njokamoni&quot;, 
&quot;UMNP-mangabey&quot;), primaryinterface = c(&quot;ecotourism&quot;, &quot;ecotourism&quot;, 
&quot;ecotourism&quot;, &quot;ecotourism&quot;, &quot;ecotourism&quot;, &quot;ecotourism&quot;, &quot;ecotourism&quot;
), secondaryinterface = c(&quot;forest use/PD&quot;, &quot;forest use&quot;, &quot;forest use&quot;, 
&quot;forest use/PD&quot;, &quot;forest use/PD/crop-raiding&quot;, &quot;forest use&quot;, 
&quot;forest use&quot;), anthrochange = c(&quot;roads, buildings&quot;, &quot;roads, campsite&quot;, 
&quot;roads, campsite&quot;, &quot;none&quot;, &quot;clearcut road, houses, crops&quot;, &quot;trail to njokamoni&quot;, 
&quot;small trails, habituated troop&quot;), species = c(&quot;RC&quot;, &quot;RC&quot;, &quot;BWC&quot;, 
&quot;SY&quot;, &quot;YB&quot;, &quot;RC&quot;, &quot;SM&quot;), geometry = structure(list(structure(c(36.883988, 
-7.844929), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.884545, 
-7.849439), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.884545, 
-7.849439), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.887065, 
-7.833501), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.891369, 
-7.832091), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.878388, 
-7.82738), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;)), structure(c(36.87753, 
-7.8232), class = c(&quot;XY&quot;, &quot;POINT&quot;, &quot;sfg&quot;))), class = c(&quot;sfc_POINT&quot;, 
&quot;sfc&quot;), precision = 0, bbox = structure(c(xmin = 36.87753, ymin = -7.849439, 
xmax = 36.891369, ymax = -7.8232), class = &quot;bbox&quot;), crs = structure(list(
    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, 
-7L), sf_column = &quot;geometry&quot;, agr = structure(c(date = NA_integer_, 
forestsite = NA_integer_, primaryinterface = NA_integer_, secondaryinterface = NA_integer_, 
anthrochange = NA_integer_, species = NA_integer_), .Label = c(&quot;constant&quot;, 
&quot;aggregate&quot;, &quot;identity&quot;), class = &quot;factor&quot;), class = c(&quot;sf&quot;, 
&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))

答案1

得分: 2

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

我与你一起使用dput()

library(sf)
library(leaflet)
library(raster)
library(sp)

# 提取坐标
df$latitudes <- st_coordinates(df$geometry)[, 2]
df$longitudes <- st_coordinates(df$geometry)[, 1]

adm <- getData('GADM', country='TZA', level=1)

popup <- paste0("<strong>人口: </strong>", 
                adm$NAME_1)

# 坦桑尼亚交互地图
TZA_MAP <- leaflet() %>%
  addTiles() %>%
  addPolygons(data=adm, weight = 2, col = 'blue', fillColor = "#FEF800", popup=popup,
              fillOpacity = 0.1) |
  addCircles(data = df ,lat= df$latitudes , lng= df$longitudes,
             label = c(df$primaryinterface, df$secondaryinterface), labelOptions = 
  labelOptions(noHide = T, direction = "bottom"),
             group = c(df$primaryinterface, df$secondaryinterface)) |
  addLayersControl(overlayGroup= c(df$primaryinterface, df$secondaryinterface),
                   options = layersControlOptions(collapsed=F))

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()

library(sf)
library(leaflet)
library(raster)
library(sp)

# Extract coordinates
df$latitudes &lt;- st_coordinates(df$geometry)[, 2]
df$longitudes &lt;- st_coordinates(df$geometry)[, 1]

adm &lt;- getData(&#39;GADM&#39;, country=&#39;TZA&#39;, level=1)

popup &lt;- paste0(&quot;&lt;strong&gt;POPULATION: &lt;/strong&gt;&quot;, 
                adm$NAME_1)

# Tanzania interactive map
TZA_MAP &lt;- leaflet() %&gt;% 
  addTiles() %&gt;%
  addPolygons(data=adm, weight = 2, col = &#39;blue&#39;, fillColor = &quot;#FEF800&quot;, popup=popup,
              fillOpacity = 0.1) |&gt; 
  addCircles(data = df ,lat= df$latitudes , lng= df$longitudes,
             label = c(df$primaryinterface, df$secondaryinterface), labelOptions = 
  labelOptions(noHide = T, direction = &quot;bottom&quot;),
             group = c(df$primaryinterface, df$secondaryinterface)) |&gt; 
  addLayersControl(overlayGroup= c(df$primaryinterface, df$secondaryinterface),
                       options = layersControlOptions(collapsed=F))

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:

确定