[R-sig-Geo] noob question: access single region from .shp file

Kent Johnson kent3737 at gmail.com
Fri Mar 10 04:22:18 CET 2017


On Thu, Mar 9, 2017 at 6:00 AM, <r-sig-geo-request at r-project.org> wrote:
>
> Message: 2
> Date: Wed, 08 Mar 2017 15:45:45 +0200
> From: Brandon Payne <payneb at post.bgu.ac.il>
> To: r-sig-geo at r-project.org
> Subject: [R-sig-Geo] noob question: access single region from .shp
>         file
> Message-ID: <m28tofq3ue.fsf at post.bgu.ac.il>
> Content-Type: text/plain
>
> How do you access a single region in a shape file?
>

See changes below.

Kent

## libraries
> library(maptools)
> library(RColorBrewer)
> colors <- brewer.pal(9, "BuGn")
> library(ggmap)
>
> ## google maps
> mapImageData3 <- get_map(location = c(lon = 35.1660235,
>                                       lat = 31.32226),
>                          color="color",
>                          source = "google",
>                          maptype = "satellite",
>                          zoom = 7)
>
> ## read shape file
> IsraelPolygon <- readShapePoly("./includes/ISR_adm/ISR_adm1.shp")
> IsraelPoints  <- fortify(IsraelPolygon)
>

IsraelPoints <- fortify(IsraelPolygon, , region='NAME_1')

## plot shapes on top of map, output to graphics device
> ggmap(mapImageData3,
>     extent = "device",
>     ylab = "Latitude",
>     xlab = "Longitude") +
>   geom_polygon(aes(x=long,
>                    y=lat,
>                    group=group),
>                data=IsraelPoints,
>                color=colors[9],
>                fil=  colors[6],
>

fill=  ifelse(IsraelPoints$id=='Golan', 'red', colors[6]),


>                alpha=0.5) +
>   labs(x="Longitude",
>        y="Latitude"

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list