Reputation: 11
I am very new to R. I managed to produce the below map with the code below.
I want to color the the rightmost polygon on the map (denoted "WestTerr" in the code) a different color than the other three polygons (denoted "Serena" in the code). I have tried various things that have all thrown errors at me. Does anyone know how to help me?
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
Upvotes: 1
Views: 546
Reputation: 23574
I do not have access to your data, so I created a sample data. Seeing your code, you created four polygons in four calls. I think you want to combine all data and draw polygons. When you use fortify()
, you see a column for id. You want to make sure that each polygon has a unique ID, if you combine your data. Once you have the data, you can do your work straightforward, I think. You can manually change the colors of polygons with 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"))
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"))
Upvotes: 2