使用ggplot从tif文件与其他图层绘制起伏数据

时间:2019-05-26 19:24:25

标签: r ggplot2 tiff

我对R非常陌生。我正在尝试制作具有以下功能的世界地图:

  1. ISEA3H网格(六边形之间的间距为6英里(我没有足够的内存来生成6英里六边形的网格;我得到的最接近的是24英里六边形)。
  2. 没有政治边界(即只有“自然边界,例如海岸线,湖泊,河流等)。我也对此有所了解。
  3. 海拔/起伏数据。这是我遇到麻烦的地方。
  4. 打印在单元格内部的单元格编号。我还没走那么远。

具体来说,我遇到的问题是我似乎无法将tif文件中的数据与世界地图“网格化”。

我正在使用dggridR软件包,该软件包将生成一个具有可变间距的世界的ISEA3H网格。我已经走得很远了。

到目前为止,这是我的代码:

library(rgdal)
library(sp)
library(dggridR)
library(mapdata)
library(rnaturalearth)
library(rnaturalearthdata)
library(raster)

# I've increased the spacing to 500 miles so the map doesn't take so long to render between iterations
dggs <- dgconstruct(projection = "ISEA", aperture = 3, topology = "HEXAGON", precision = 7, spacing = 500, metric = FALSE)

# Save a fairly large image at the end of it
jpeg("mymap.jpg", width = 9000, height = 6000, quality = 95)

# I'm using the physical data from https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/10m_physical.zip

# and the raster data from https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/raster/NE2_HR_LC_SR_W.zip

world <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_land")
rivers <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_rivers_lake_centerlines")
ocean <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_ocean")
lakes <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_lakes")
coast <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_coastline")

rel <- raster("/home/mario/Downloads/data/NE2_HR_LC_SR_W/NE2_HR_LC_SR_W.tif")
rel_spdf <- as(rel, "SpatialPixelsDataFrame")
rel <- as.data.frame(rel_spdf)

# use grid of the entire earth
sa_grid <- dgearthgrid(dggs,frame=TRUE, wrapcells=TRUE)

# actually plot the data
p<- ggplot() +

    # what should be the relief layer
    geom_tile(data = rel, aes(x = "x", y = "y")) +

    # the world
    geom_polygon(data=world, aes(x=long, y=lat, group=group), fill=NA, color="black") +

    # lakes
    geom_polygon(data=lakes, aes(x = long, y = lat, group = group), fill = '#ADD8E6') +

    # more lakes
    geom_path(data=lakes, aes(x = long, y = lat, group = group), color = 'blue') +

    # rivers
    geom_path(data=rivers, aes(x=long, y=lat, group=group), alpha=0.4, color="blue") +

    # coastline
    geom_path(data=coast, aes(x = long, y = lat, group = group), color = 'blue') +

    # hexagonal grid
    geom_polygon(data=sa_grid, aes(x=long, y=lat, group=group), fill="white", alpha=0.4) +

    # hexagonal grid
    geom_path(data=sa_grid, aes(x=long, y=lat, group=group), alpha=0.4, color="grey") +

    # some necessary r code that I don't understand
    coord_equal()

# change from flat map to globe
p+coord_map("ortho", orientation = c(41.0082, 28.9784, 0))+
  xlab('')+ylab('')+
  theme(axis.ticks.x=element_blank())+
  theme(axis.ticks.y=element_blank())+
  theme(axis.text.x=element_blank())+
  theme(axis.text.y=element_blank())+
  ggtitle('World map')

# finish writing to jpeg
dev.off()

据我所知:https://imgur.com/y9LPqVS

上面的代码目前不合时宜,并且已经过去4个小时了。它仍然在机器内存的范围内,所以这是一个好兆头。

在地球的地形图上投影测地网格的惯用方式是什么?到目前为止,我如何才能将Naturalearth tif文件中的地形数据与我的代码一起包含?

0 个答案:

没有答案