在ggplot2(R)中着色多边形

时间:2016-09-02 21:49:57

标签: r ggplot2 ggmap

我是R.的新手。我设法使用下面的代码生成下面的地图。

Map of Sampled areas in Masai Mara National Reserve

我想为地图上最右边的多边形(在代码中表示为" WestTerr")使用与其他三个多边形不同的颜色(在代码中表示为" Serena") 。我已经尝试了各种各样的东西,这些东西都给我带来了错误。有谁知道如何帮助我?

library(ggmap)
library(RgoogleMaps)
library(foreign)
library(raster)
library(sp)
library(rgeos)
library(spatstat)
library(maptools)
library(rgdal)
library(ggplot2)

CenterOfMap <- geocode("-1.4, 35.08")

MasaiMara <- get_map(c(lon=CenterOfMap$lon, lat=CenterOfMap$lat), zoom = 10, 
                     maptype = "roadmap", source = "google")

MasaiMaraMap <- ggmap(MasaiMara)

setwd("Territories_Jenna/")

SouthTerr <- readOGR(".","SouthMCP")
SouthTerr <- spTransform(SouthTerr, CRS("+proj=longlat +datum=WGS84"))
fortify(SouthTerr)

HZTerr <- readOGR(".","HZMCP")
HZTerr <- spTransform(HZTerr, CRS("+proj=longlat +datum=WGS84"))
fortify(HZTerr)

SandHZ <- gUnion(SouthTerr, HZTerr)
SandHZ <- spTransform(SandHZ, CRS("proj=longlat +datum=WGS84"))
fortify(SandHZ)

MapwithSandHZ <- MasaiMaraMap +
                 geom_polygon(aes(x=long, y=lat, group=group),
                              fill = "black", size=.7, color="black",
                              data=SandHZ, alpha=0)

NorthTerr <- readOGR(".","NorthMCP")
NorthTerr <- spTransform(NorthTerr, CRS("+proj=longlat +datum=WGS84"))
fortify(NorthTerr)

MapwithNorth <- MasaiMaraMap +
                geom_polygon(aes(x=long, y=lat, group=group),
                             fill = "black", size=.7, color="black",
                             data=NorthTerr, alpha=0)

Serena <- gUnion(SandHZ, NorthTerr)
Serena <- spTransform(Serena, CRS("proj=longlat +datum=WGS84"))
fortify(Serena)

MapwithSerena <- MasaiMaraMap +
                 geom_polygon(aes(x=long, y=lat, group=group),
                              fill = "black", size=.7, color="black",
                              data=Serena, alpha=0)

WestTerr <- readOGR(".","WestMCP")
WestTerr <- spTransform(WestTerr, CRS("+proj=longlat +datum=WGS84"))
fortify(WestTerr)

MapwithWest <- MasaiMaraMap +
               geom_polygon(aes(x=long, y=lat, group=group), 
                            fill = "red", size=.7, color="red",
                            data=WestTerr, alpha=0)

AllTerrs <- gUnion(Serena, WestTerr)
AllTerrs <- spTransform(AllTerrs, CRS("proj=longlat +datum=WGS84"))
fortify(AllTerrs)

MapwithAllTerrs <- MasaiMaraMap +
                   geom_polygon(aes(x=long, y=lat, group=group),
                                fill = "red", size=.7, color="red",
                                data=AllTerrs, alpha=0)

MapwithAllTerrs

1 个答案:

答案 0 :(得分:2)

我无法访问您的数据,因此我创建了一个示例数据。看到您的代码,您在四次调用中创建了四个多边形。我想你想要结合所有数据并绘制多边形。使用fortify()时,会看到一列id。如果组合数据,则需要确保每个多边形都具有唯一ID。一旦掌握了数据,我认为你可以直截了当地开展工作。您可以使用scale_fill_manual()

手动更改多边形的颜色
library(ggmap)
CenterOfMap <- geocode("-1.4, 35.08")

MasaiMara <- get_map(c(lon=CenterOfMap$lon, lat=CenterOfMap$lat), zoom = 10, 
                     maptype = "roadmap", source = "google")

ggmap(MasaiMara) +
geom_polygon(data = mydf, aes(x = long, y = lat, group = id, fill = factor(id))) +
scale_fill_manual(values = c("red", "red", "blue"))

enter image description here

DATA

mydf <-structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L), long = c(34.97, 34.95, 34.95, 34.97, 34.97, 
35, 34.82, 34.83, 35, 35, 35.2, 35.1, 35.1, 35.2, 35.2), lat = c(-1.38, 
-1.38, -1.4, -1.4, -1.38, -1.6, -1.6, -1.7, -1.7, -1.6, -1.43, 
-1.43, -1.5, -1.5, -1.43)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -15L), .Names = c("id", "long", "lat"), spec = structure(list(
cols = structure(list(id = structure(list(), class = c("collector_integer", 
"collector")), long = structure(list(), class = c("collector_double", 
"collector")), lat = structure(list(), class = c("collector_double", 
"collector"))), .Names = c("id", "long", "lat")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))