简体   繁体   English

在 R 中绘制 GeoTIFF 栅格

[英]Plotting a GeoTIFF raster in R

I am trying to plot a gridded population count GeoTIFF raster downloaded from here .我正在尝试绘制从此处下载的网格化人口计数 GeoTIFF 栅格。

library(raster)

#----------------------------#
# Set your working directory #
#----------------------------#

setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # RStudio IDE preferred

getwd() # Path to your working directory

# Import the GeoTIFF file into R workspace

WorldPop <- raster("nga_ppp_2020_1km_Aggregated_UNAdj.tif")

WorldPop

#---------------------------#
# Data plotted in log-scale #
#---------------------------#

tempcol <- colorRampPalette(c("lightblue", "skyblue", "blue", "yellow", "orange", "red", "darkred"))

plot(log(WorldPop), main = "2020 UN-Adjusted Population Count (log-scale) \n (each grid cell is 1 km x 1 km)", col=tempcol(100), legend.width=2, legend.shrink=1, legend.args=list(text='log(Persons)', side=4, font=2, line=2.5, cex=0.8), axes=T)

#--------------------------------#
# Data plotted in absolute-scale #
#--------------------------------#

plot(WorldPop, main = "2020 UN-Adjusted Population Count (absolute-scale) \n (each grid cell is 1 km x 1 km)", col=tempcol(100), legend.width=2, legend.shrink=1, legend.args=list(text='Persons', side=4, font=2, line=2.5, cex=0.8), axes=T)

Plot 1 (log-scale)图 1(对数刻度)

在此处输入图片说明

Plot 2 (absolute-scale)图 2(绝对尺度)

在此处输入图片说明

I like the Plot 1 (data in log-scale) but Plot 2 (data in absolute-scale) is not showing any variations in color.我喜欢 Plot 1(对数刻度的数据),但 Plot 2(绝对刻度的数据)没有显示任何颜色变化。 How can I make the plot of data in absolute-scale look similar to the one in log-scale?如何使绝对尺度的数据图看起来与对数尺度的数据图相似?

I am open to using other packages (ggplot2 etc.) or other color palates as long as my plot can distinguish densely populated areas from rural areas.我愿意使用其他包(ggplot2 等)或其他颜色的口味,只要我的情节可以区分人口稠密地区和农村地区。 When I used a different GIS tool called Panoply my favorite color palate was something called seminf-haxby.cpt (found here ).当我使用一个名为 Panoply 的不同 GIS 工具时,我最喜欢的颜色是seminf-haxby.cpt (在这里找到)。 It looks something like this它看起来像这样

在此处输入图片说明

I am trying to replicate that in R but the plot in absolute-scale isn't looking good.我试图在 R 中复制它,但绝对比例的情节看起来并不好。 Any tips or advice for plotting tif rasters in R?在 R 中绘制 tif 光栅的任何提示或建议?

You can set breaks, something like this:您可以设置休息时间,如下所示:

Example data示例数据

url <- "https://data.worldpop.org/GIS/Population/Global_2000_2020_1km_UNadj/2020/NGA/nga_ppp_2020_1km_Aggregated_UNadj.tif"
fname <- basename(url)
if (!file.exists(fname)) download.file(url, fname, mode="wb")

Solution解决方案

library(terra)
r <- rast(fname)
plot(r, col=rev(rainbow(10, end=0.7)), breaks=c(0, 10, 25, 50, 100, 250, 1000, 100000))

在此处输入图片说明

Now your second question (it is better to not ask two rather different questions at the same time).现在你的第二个问题(最好不要同时问两个相当不同的问题)。

The function below extracts the colors from the palette image in your question, and should also work for other palette images organized like yours.下面的函数从您问题中的调色板图像中提取颜色,并且也适用于像您这样组织的其他调色板图像。

getPal <- function(f) {
    x <- rast(f)
    u <- unique(values(x))
    hex <- rgb(u[,1], u[,2], u[,3], maxColorValue = 255)
    colorRampPalette(hex)
}

pal <- getPal("https://i.stack.imgur.com/E4d85.png")

par(mar=c(0,0,0,0))
barplot(rep(1, 25), col=pal(25), space=0)

在此处输入图片说明

An alternative way to apply breaks is to first use classify.应用中断的另一种方法是首先使用分类。 I remove the first color (white)我去掉第一个颜色(白色)

x <- classify(r, c(0, 10, 25, 50, 100, 250, 1000, 100000))
plot(x, col=pal(8)[-1])

在此处输入图片说明

You could change the labels, for example like this您可以更改标签,例如像这样

levs <- levels(x)[[1]]
levs[7] <- "> 1000"
levels(x) <- levs

To use this palette in your R code you can create it like this (remove '#FFFFFF' if you do not want to start with white)要在您的 R 代码中使用此调色板,您可以像这样创建它(如果您不想以白色开头,请删除“#FFFFFF”)

ramp <- c('#FFFFFF', '#D0D8FB', '#BAC5F7', '#8FA1F1', '#617AEC', '#0027E0', '#1965F0', '#0C81F8', '#18AFFF', '#31BEFF', '#43CAFF', '#60E1F0', '#69EBE1', '#7BEBC8', '#8AECAE', '#ACF5A8', '#CDFFA2', '#DFF58D', '#F0EC78', '#F7D767', '#FFBD56', '#FFA044', '#EE4F4D')
pal <- colorRampPalette(ramp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM