[英]How to plot data on map without coordinates?
我正在嘗試 plot dataframe 之類的:
code name description estimate
0 Australia Vegetables 854658
0 Australia Fruit 667541
1 New South Wales Vegetables 45751
1 New South Wakes Fruit 77852
2 Victoria Vegetables 66211
2 Victoria Fruit 66211
.
.
.
對於澳大利亞的每個地區,都有多行具有不同的描述。 我可以使用哪些包到 plot 和 map 估計沒有坐標?
我嘗試使用 ggplot2 教程中提到的sf
的ggplot
和ozmaps
,我過濾 dataframe 僅用於水果,但出現錯誤消息: stat_sf requires the following missing aesthetics: geometry
我試過的代碼:
ggplot() +
geom_sf(oz_states,mapping=aes())+
geom_sf(df,mapping=aes()) +
coord_sf()
我找到的方法都是需要經緯度到plot數據map,我試過ggmaps
或者geom_ploygon
但是沒有找到正確的方法。 有沒有可能的方法 plot map 只有區域標簽?
這就是我 plot 的畫面,這也是預期的 plot 使用 r :
所以基本上,你的第一個問題是你在 ozmaps package 中調用了錯誤的ozmaps
。 它是ozmap_states
,同時你叫你的oz_states
我想出了這個解決方案,我認為它可以滿足您的需求並提升它。
df <- data.frame(code = rep(c(0,1,2), 2), name = rep(c("Australia", "New South Wales", "Victoria"), 2), description = rep(c("Vegetables", "Fruit"), 3), count =
c(854658, 45751, 66211, 667541, 77852, 66211))
library(tidyverse)
library(sf)
library(ozmaps)
library(leaflet)
library(tmap)
states_full <- right_join(df, ozmap_states, by = c("name" = "NAME"))
data <- states_full %>%
filter(description == "Fruit") %>%
select(name, geometry, count)
ozmap1 = tm_shape(ozmap_states) +tm_polygons()
tmap_mode("view")
ozmap1 + tm_shape(st_as_sf(data)) + tm_fill(col = "count")
基本上,不用使用我從您的數據創建的示例 dataframe,您只需在正確的連接中使用您的數據。 您還可以選擇在過濾器 function 中是否需要水果或蔬菜。
tmap package 是一個映射 package 可以使交互式 leaflet 像地圖。
您可以在這里查看一些教程: https://geocompr.robinlovelace.net/adv-map.html
最終解決方案看起來像這樣。
注意:此解決方案使用 lng/lat,但它直接從 ozmaps package 中的 oz state 映射的形狀文件中提取它,因此滿足了問題的需要。
當您添加更多數據時,將根據數量對澳大利亞更多地區進行着色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.