我正在為其他人制作的地圖改編代碼,該地圖使用包concaveman
從點生成凹殼。目標是在海洋中繪制許多不同的多邊形,并通過分組變數對它們進行顏色編碼。該代碼非常適合制作所有多邊形的地圖并按身份對它們進行顏色編碼:
library(sf)
library(concaveman)
library(data.table)
library(ggplot2)
dat <- data.table(longitude = c(-131.319783, -131.141266, -131.08165, -131.079066, -130.894966,
-131.063783, -131.10855, -131.215533, -131.189816, -131.14565,
-131.200866, -131.046466, -130.94055, -130.928983, -130.7513,
-130.8406, -130.833433, -130.830666, -130.82205, -130.89, -63.3666666666667,
-63.3666666666667, -63.1666666666667, -64.1833333333333, -63.3166666666667,
-63.3, -63.85, -63.9333333333333, -63.9333333333333, -63.5833333333333,
-63.5833333333333, -63.7, -63.7, -63.2833333333333, -63.5833333333333,
-63.95, -64.1833333333333, -63.8833333333333, -63.8, -63.2166666666667,
-5.6788, -5.4408, -5.6835, -5.424, -5.6475, -5.4371, -5.6181,
-5.4446, -5.6753, -5.4366, -5.6746, -5.4448, -5.6642, -5.4411,
-5.666, -5.4408, -5.624, -5.4321, -5.6806, -5.4473),
latitude = c(52.646633, 52.589683, 52.556516, 52.559816, 52.402916, 52.5983,
52.554216, 52.550883, 52.539166, 52.658216, 52.627966, 52.481733,
52.486033, 52.469033, 52.469166, 52.261833, 52.292133, 52.301066,
52.3523, 52.366966, 48.4666666666667, 48.4666666666667, 48.65,
49.0166666666667, 48.8166666666667, 48.8166666666667, 49.1, 48.8666666666667,
48.8666666666667, 48.8, 48.8166666666667, 48.4833333333333, 48.4833333333333,
48.8, 48.8166666666667, 48.8833333333333, 49.05, 49.0833333333333,
48.7166666666667, 48.6666666666667, 54.7201, 54.6033, 54.7191,
54.5733, 54.7225, 54.5923, 54.7261, 54.6076, 54.719, 54.5978,
54.7195, 54.6108, 54.7204, 54.6062, 54.7214, 54.5923, 54.7275,
54.592, 54.7207, 54.6188),
group = c(rep('NEPac',20),rep('NWAtl',20),rep('NEAtl',20))
)
split <- split(dat, dat$group)
split.sf <- lapply(split, st_as_sf, coords = c("longitude", "latitude"))
concave <- lapply(split.sf, concaveman, concavity = 3, length_threshold = 2)
concave.binded <- do.call('rbind', concave)
concave.spdf <- as_Spatial(concave.binded)
ggplot()
geom_polygon(data = concave.spdf,
aes(x = long, y = lat, group = group, fill = group, color = group))
但是,我不知道如何用其他任何東西填充多邊形group
。這是我的嘗試:
concave.spdf$ocean <- c('P','A','A')
ggplot()
geom_polygon(data = concave.spdf,
aes(x = long, y = lat, group = group, fill = ocean, color = ocean))
引發此錯誤:Error in FUN(X[[i]], ...) : object 'ocean' not found
我認為問題是split
在傳遞給時按身份對多邊形進行分組concaveman
,但如果我改變它,它們將無法正確繪制(因為不同多邊形的點將被合并)。如何保持單獨繪制多邊形但通過分組變數為它們著色?(如果可能的話,出于美學原因,我更愿意concaveman
在真實情節中堅持[這比這個reprex復雜得多]——我知道如果我使用不同的方法來繪制多邊形,這會更容易。)
uj5u.com熱心網友回復:
最簡單的方法是添加一個scale_fill_manual
:
ggplot()
geom_polygon(data = concave.spdf,
aes(x = long, y = lat, group = group, fill = group))
scale_fill_manual(values = c("red", "green", "blue"),
labels = c("Ocean 1", "Ocean 2", "Ocean 3"))
可以說,更好的方法是轉換為簡單的特征集合,您可以在其中添加您喜歡的任何列,并自動繪制geom_sf
concave.spdf <- st_as_sf(concave.spdf)
concave.spdf$ocean <- c("Ocean 1", "Ocean 2", "Ocean 3")
ggplot(concave.spdf)
geom_sf(aes(fill = ocean))
請注意,這也會自動給出正確的坐標比例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/469531.html