R-轮廓图中的不同颜色区域

时间:2019-04-12 17:33:13

标签: r plot maps

我有2个不同的遮罩,我想在地图上绘制轮廓。 这些蒙版基本上为[180 x 360],蒙版填充为1,在某些情况下其余为0或NaN。 这是我用于遮罩的文件:Mask1Mask2。 这是我尝试过的代码:

library(raster)
library(pracma)



R_Caribe <- read.delim("R_Caribe.txt", header=FALSE)
mask1 <- as.matrix(R_Caribe)

mask1[mask1 == 0] <- NA 


R_CUENCA <- read.delim("R_CUENCA.txt", header=FALSE)
mask2 <- as.matrix(R_CUENCA)

mask2[mask2 == 0] <- NA

###########################################################
#I need to rotate the masks so they can plot properly
############################################################

r_1 = raster(pracma::circshift(mask1, c(0,181)))
extent(r_1) <- c(-180,180,-90,90)

r_2 <- raster(pracma::circshift(mask2, c(0,181)))
extent(r_2) <- c(-180,180,-90,90)



###########################################################
#This is to get the coastlines
############################################################

mex <- getData("GADM", country = "MEX", level = 0)
us <- getData("GADM", country = "USA", level = 0)


###########################################################
# Plot
############################################################
plot(r_1, xlim = c(-130, -20), ylim = c(5 ,35))
plot(r_2,add = TRUE)
plot(mex, add = TRUE)
plot(us, add = TRUE)}

使用该代码,我得到以下地图: enter image description here

我想要获取的是地图,但是每个蒙版都应具有特定的颜色,并用黑线勾勒出轮廓,最后在其上贴上名称,即“ mask1”,“ mask2”。

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题,我使用了罗伯特的答案from here

    library(raster)
    library(pracma)
    library(sp)
    library(maps)


    #READ THE MASKS

    R_Caribe <- read.delim("E:/PCT/Datos/Mascaras_humedad/R_Caribe.txt", header=FALSE)
    mask1 <- as.matrix(R_Caribe)

    mask1[mask1 == 0] <- NA


    R_CUENCA <- read.delim("E:/PCT/Datos/Mascaras_humedad/R_CUENCA.txt", header=FALSE)
    mask2 <- as.matrix(R_CUENCA)

    mask2[mask2 == 0] <- NA


    R_PACIFICO <- read.delim("E:/PCT/Datos/Mascaras_humedad/R_PACIFICO.txt", header=FALSE)
    mask3 <- as.matrix(R_PACIFICO)

    mask3[mask3 == 0] <- NA


    R_ROFL <- read.delim("E:/PCT/Datos/Mascaras_humedad/R_ROFL.txt", header=FALSE)
    mask4 <- as.matrix(R_ROFL)

    mask4[mask4 == 0] <- NA

#ROTATE
    r_1 = raster(pracma::circshift(mask1, c(0,181)))
    extent(r_1) <- c(-180,180,-90,90)

    r_2 <- raster(pracma::circshift(mask2, c(0,181)))
    extent(r_2) <- c(-180,180,-90,90)

    r_3 <- raster(pracma::circshift(mask3, c(0,181)))
    extent(r_3) <- c(-180,180,-90,90)

    r_4 <- raster(pracma::circshift(mask4, c(0,181)))
    extent(r_4) <- c(-180,180,-90,90)


#CREATE POLYGONS
    rr1 <- r_1 > -Inf
    caribe <- rasterToPolygons(rr1, dissolve = TRUE)


    rr2 <- r_2 > -Inf
    cuenca <- rasterToPolygons(rr2, dissolve = TRUE)


    rr3 <- r_3 > -Inf
    pacifico <- rasterToPolygons(rr3, dissolve = TRUE)


    rr4 <- r_4 > -Inf
    rofl <- rasterToPolygons(rr4, dissolve = TRUE)


    #..............................................................................
    #                           P L O T S
    #..............................................................................
    par(mar=c(4.9, 3.9, 4.1, 9.1), xpd=TRUE)

    plot(caribe, xlim = c(-130, -20), ylim = c(10 ,35), lwd=5, border='black',col = "plum", xlab="Lon", ylab="Lat")
    plot(pacifico,border='black', lwd=5,col = "coral3",add = TRUE)
    plot(rofl, border='black', lwd=5,col = "deepskyblue",add =TRUE)
    plot(cuenca, border='black', lwd=5,col = "yellow",add =TRUE)
    axis(side=1, labels=TRUE)
    axis(2, labels=TRUE)
    legend("topright", inset=c(-0.2,0),legend=c("Caribe", "Pacífico", "R.T.","Cuenca"), pt.bg=c("plum", "coral3","deepskyblue","yellow"), cex=1.5,box.lty=0, pch = 22, col=c("black","black"))
    map("world",add=TRUE,fill=FALSE,xlim = c(-130, -20), ylim = c(10 ,35))

最终结果是这个

enter image description here 我尝试使用ggplot,但无法弄清楚如何使用它,所以如果有人知道,请告诉我。

  [1]: https://gis.stackexchange.com/questions/187798/create-polygons-of-the-extents-of-a-given-raster-in-r