为什么 ggplot 和 geom_tile 在地图上创建线条?
why ggplot and geom_tile create lines on the map?
数据:https://github.com/yuliaUU/data/blob/main/test.csv
griddf <- read_csv("test.csv")
创建地图:
world <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf") # add continents
ggplot()+
geom_tile(data = Data |> dplyr::filter(Model.1=="RF"), aes(x = Lon, y = Lat, fill= value/1000))+geom_sf(data=world)+
viridis:: scale_fill_viridis(option = "H", na.value = NA) +
labs(fill="Probability")+
facet_wrap(~ Model.1)
我的问题是它创建了一个带有“线条”的地图,我不明白为什么。我知道这与我使用的不规则网格有关(所有网格单元的面积应该相等)
当我添加不同的投影时:
+ coord_sf(crs = '+proj=moll')
我什么也没画
您基本上是自己回答了问题 - 您的数据过于细化。为了获得更“完整”的外观,您可能需要对值进行二维插值。在这里,我使用 akima::interp
,但还有其他功能 - 这里不是讨论哪个最好用的地方。
library(ggplot2)
griddf <- read.csv(url("https://raw.githubusercontent.com/yuliaUU/data/main/test.csv"))
world <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf") # add continents
val_interpol <- with(griddf, akima::interp(Lon, Lat, value, xo = -180:180, yo = -90:90))
#> Warning in akima::interp(Lon, Lat, value, xo = -180:180, yo = -90:90): collinear
#> points, trying to add some jitter to avoid colinearities!
#> Warning in akima::interp(Lon, Lat, value, xo = -180:180, yo = -90:90): success:
#> collinearities reduced through jitter
## thanks Akrun
## matrix doesn't allow negative values for subsetting
d1 <- expand.grid(x = 1:361, y = 1:181)
out <- transform(d1, z = val_interpol$z[as.matrix(d1)])
out$x <- out$x-181
out$y <- out$y-91
ggplot()+
geom_raster(data = out , aes(x = x, y = y, fill= z), interpolate = TRUE)+
geom_sf(data=world)+
labs(title=paste0("Swordfish Probability of Occurance"),
x="", y="", subtitle="data from 2000-present (0.5x0.5 grid)")+
viridis:: scale_fill_viridis(option = "H", na.value = "black")
由 reprex package (v2.0.1)
于 2022-05-06 创建
数据:https://github.com/yuliaUU/data/blob/main/test.csv
griddf <- read_csv("test.csv")
创建地图:
world <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf") # add continents
ggplot()+
geom_tile(data = Data |> dplyr::filter(Model.1=="RF"), aes(x = Lon, y = Lat, fill= value/1000))+geom_sf(data=world)+
viridis:: scale_fill_viridis(option = "H", na.value = NA) +
labs(fill="Probability")+
facet_wrap(~ Model.1)
我的问题是它创建了一个带有“线条”的地图,我不明白为什么。我知道这与我使用的不规则网格有关(所有网格单元的面积应该相等)
当我添加不同的投影时:
+ coord_sf(crs = '+proj=moll')
我什么也没画
您基本上是自己回答了问题 - 您的数据过于细化。为了获得更“完整”的外观,您可能需要对值进行二维插值。在这里,我使用 akima::interp
,但还有其他功能 - 这里不是讨论哪个最好用的地方。
library(ggplot2)
griddf <- read.csv(url("https://raw.githubusercontent.com/yuliaUU/data/main/test.csv"))
world <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf") # add continents
val_interpol <- with(griddf, akima::interp(Lon, Lat, value, xo = -180:180, yo = -90:90))
#> Warning in akima::interp(Lon, Lat, value, xo = -180:180, yo = -90:90): collinear
#> points, trying to add some jitter to avoid colinearities!
#> Warning in akima::interp(Lon, Lat, value, xo = -180:180, yo = -90:90): success:
#> collinearities reduced through jitter
## thanks Akrun
## matrix doesn't allow negative values for subsetting
d1 <- expand.grid(x = 1:361, y = 1:181)
out <- transform(d1, z = val_interpol$z[as.matrix(d1)])
out$x <- out$x-181
out$y <- out$y-91
ggplot()+
geom_raster(data = out , aes(x = x, y = y, fill= z), interpolate = TRUE)+
geom_sf(data=world)+
labs(title=paste0("Swordfish Probability of Occurance"),
x="", y="", subtitle="data from 2000-present (0.5x0.5 grid)")+
viridis:: scale_fill_viridis(option = "H", na.value = "black")
由 reprex package (v2.0.1)
于 2022-05-06 创建