using ggmap to plot points randomly distributed within boundaries of shape file - r

Is it possible to use ggplot2 / ggmap to plot geom_points randomly within a given spatial area defined by a shapefile?
I considered geom_jitter, however I need the plots to be randomly distributed and not cross spatial borders.
Sample data shamelessly borrowed from #matthiash here.
library(rgdal)
library(ggmap)
# Get shapefile with Drammen municipality borders
tmpzip<-tempfile()
tmpdir<-tempfile()
dir.create(tmpdir)
download.file("http://www.kartverket.no/Documents/Kart/N50-N5000%20Kartdata/33_N5000_shape.zip",tmpzip)
unzip(tmpzip, exdir=tmpdir)
kommune <- readOGR(dsn=tmpdir, layer="NO_AdminOmrader_pol")
kommune<-kommune[kommune$NAVN=="Drammen",]
kommune<-spTransform(kommune, CRS("+init=epsg:4326"))
dat<-fortify(kommune)
#get the base map
map <- get_map(location = "Drammen",
maptype = "watercolor", source = "stamen", zoom = 11)
Below code plots the base map with region id 154 from the shapefile plotted on top.
ggmap(map, extent = "normal", maprange = TRUE)+
geom_polygon(data = dat,
aes(long, lat, group = group),
fill = "orange", colour = "red", alpha = 0.2)
What I'd like to do is plot 10 points randomly within the shapefile region defined by dat$id==154

Ok, I sorted it out. The solution is in spsample() in package "raster".
d<-data.frame(id=NA,x=NA,y=NA)
l<-data.frame(id=154,n=10)
for (i in unique(l$id)){
temp<-spsample(kommune[which(kommune$OBJECTID==i),],n=l[l$id==i,"n"],type="random")
temp<-as.data.frame(temp)
temp$id<-i
d<-rbind(d,temp[,c("id","x","y")])
}
d<-d[-1,] #drop the first empty row
ggmap(map, extent = "normal", maprange = T)+
geom_polygon(data = dat,
aes(long, lat, group = group),
fill = "blue", colour = "yellow", alpha = 0.1)+
geom_point(aes(x = x, y = y), data = d[which(d$id==154),], alpha = .9,show.legend = T)

Related

Plotting points with OpenstreetMap in R

I tried to get Berlin map from OpenStreetMap and then plot some locations on it (they should look like points, not markers).
I got the map based on the data I have cause I don't want a whole map, but area where includes all locations. However when I plot the points on the map, it delivers an error message:
non-numeric argument to binary operator
My code:
library(ggplot2)
library(OpenStreetMap)
# creating a sample data.frame with lat/lon points
lon <- read.csv("Data.csv", header = TRUE, colClasses=c("NULL", "NULL", NA))
lat <- read.csv("Data.csv", header = TRUE, colClasses=c("NULL", NA, "NULL"))
df <- as.data.frame(cbind(lon,lat))
# getting the map
mapberlin <- openmap(c(max(lat)+0.03,min(lon)-0.03), c(min(lat)-0.03,max(lon)+0.03), zoom = NULL,type = 'osm')
# plotting the map with some points on it and draw line between points
plot(mapberlin) + geom_point(data = df, aes(x = lon, y = lat, fill = 'red'), size = 2, shape = 21)
+geom_line(data = df, aes(x = lon, y = lat), color = 'blue') +
+guides(fill=FALSE, size=FALSE)

Error: Aesthetics must be either length 1 or the same as the data (4)

I am working with ggmap. the goal is to plot coordinate points on the map and label the points with their names. I have data frame with name, longitude and latitude.
The data looks like:
df <- structure(list(Station.Area = c("Balbriggan", "Blanchardstown",
"Dolphins Barn", "Donnybrook", "Dun Laoghaire", "Finglas"), Latitude = c(53.608319,
53.386813, 53.333532, 53.319259, 53.294396, 53.390325), Longitude = c(-6.18208,
-6.377197, -6.29146, -6.232017, -6.133867, -6.298401)), .Names =c("Station.Area","Latitude", "Longitude"), row.names = c(NA, 6L), class = "data.frame")
The code I wrote is as below:
library(ggmap)
library(ggplot2)
dub_map <- get_map(location = "Dublin", zoom = "auto", scale="auto", crop = TRUE, maptype = "hybrid")
ggmap(dub_map) +`
geom_point(data = df, aes(x = Longitude, y = Latitude,
fill = "green", alpha =` `0.8, size = 5, shape = 21)) +`
guides(fill=FALSE, alpha=FALSE, size=FALSE)+
geom_text(label=df$Station.Area)+
scale_shape_identity()
But i am getting
Error: Aesthetics must be either length 1 or the same as the data (4): label
I have tried to put various aesthetics in geom_text like size,color,x & Y but it still gives out same error.
Am i doing it correctly for my goal? Please help.
Getting this without geom_text now I just want to label the points
There are a couple of things not quite right in your code.
For the geom_point you only want the x and y in your aes. The other arguments
should be outside, giving
geom_point(data = df, aes(x = Longitude, y = Latitude),
fill = "green", alpha =0.8, size = 5, shape = 21)
Also the label for the geom_text should be inside aes. However,
as there is no data, x or y at a higher level, then geom_text
will not find the label variable or the positions of where to place the labels. So you also need to include these in the call
geom_text(data=df, aes(x = Longitude, y = Latitude, label=Station.Area))
However, you can omit some of this repetition by using the base_layer argument
of ggmap:
ggmap(dub_map,
base_layer = ggplot(data=df, aes(x = Longitude,
y = Latitude,
label=Station.Area))) +
geom_point(fill = "green", alpha =0.8, size = 5, shape = 21) +
geom_text()

Multiple Points on Map

I want to plot a map with some points on it. I tried this code:
lon <- c(103.25,103.28)
lat <- c(3.80, 3.78)
df <- as.data.frame(cbind(lon,lat))
Getting the map:
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 12,maptype = "satellite", scale = 3)
Plotting the map with some points on it:
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8),size = 5, shape = 21) +guides(fill=FALSE, alpha=FALSE, size=FALSE)
Based on this code, the same color of points appear. My question is, I want to create multiple color of points on the map. Kindly assist, your help is highly appreciated. Thank you.
You need to add a categorical variable (what should the colors express?) to govern the color aesthetics:
#create some dummy data
df$coloringCategory <- rep(c("A","B"),length(df$lat)/2)
#in ggplot include the categorical variable
geom_point(data = df, aes(x = lon, y = lat, color= coloringCategory, alpha = 0.8),size = 5, shape = 21)

How to plot data on a map without using Google map (image)?

I have a csv file with variables name "Latitude","Longitude","PM10 concentration". You can download data here. I want to plot PM10 data on a map of South Korea according to their latitude and Longitude. Also I want to show them as bubble with different size and color.
Following this example I have already plotted PM10 data on Google Map. But now I want do this without using Google map rather by creating spatial object or in any other way.
I tried to write some code but I have download the spatial data for administration area (GADM) of South Korea. But I am not sure that approach is right or wrong.
library(rgdal)
library(ggplot2)
library(maptools)
map<-readOGR('D:/BACKUP/R/GSTAT/R File/shape file korea map',layer ='KOR_adm2')
summary(kmap)
EPSG<-make_EPSG()
EPSG[grepl("WGS 84$", EPSG$note), ]
kmap84<-spTransform(kmap, CRS("+init=epsg:4326"))
kmaps<-fortify(kmap84)
I don't understand what should I do next.
Here's an example:
library(raster)
library(ggplot2)
download.file("https://docs.google.com/uc?id=0ByY3OAw62EShakxJZkplOXZ0RGM&export=download", tf <- tempfile(fileext = ".csv"))
df <- read.csv(tf, row.names = 1)
skorea <- getData("GADM", country = "South Korea", level = 2)
skorea <- fortify(skorea)
ggplot() +
geom_map(data = skorea, map = skorea, aes(x = long, y = lat, map_id = id, group = group),
fill = NA, colour = "black") +
geom_point(data = df, aes(x = LON, y = LAT, size = PM10), colour = "red", alpha = .5) +
scale_size(range = c(1, 5))

Plot points outside grid as arrows pointing to data with ggplot2 in R

I am generating maps with world-scale data, and then zooming in to certain regions. On the zoomed-in view, I would like to show that there are other data points outside the bounding box, by putting arrowheads that point from the center of the box to where the data point is in the outside world.
Note: I do not need it to be a "great circle" path, just XY vectors in Mercator projection, because I imagine this will be useful for "normal" plots as well.
As an example, here is the world map showing the extent of the data:
And here is the zoomed in view, with magenta arrows manually added to show what I would like to generate.
Below is the code and data I am using to generate these two basic plots. What I need is a way to generate the arrowheads.
require(ggplot2)
te = structure(list(lat = c(33.7399, 32.8571, 50.2214, 36.96263, 33.5835,
33.54557, 47.76147, 48, 59.40289, 35.93411, 32.87962, 38.3241,
50.03844, 37.44, 50.07774, 50.26668, 36.5944), lng = c(-118.37608,
-117.25746, -5.3865, -122.00809, -117.86159, -117.79805, -124.45055,
-126, -146.35157, -122.931472, -117.25285, -123.07331, -5.26339,
25.4, -5.709894, -3.86828, -121.96201)), .Names = c("lat", "lng"
), class = "data.frame", row.names = c(NA, -17L))
all_states = map_data("world")
# world version:
wp = ggplot() +
geom_polygon(data = all_states, aes(x = long, y = lat, group = group), colour = "gray",
fill = "gray") +
coord_cartesian(ylim = c(0, 80), xlim = c(-155, 45)) +
geom_point(data = te, aes(x = lng, y = lat), color = "blue", size = 5,alpha = 0.6)
print(wp)
#states plot
sp = ggplot() +
geom_polygon(data = all_states, aes(x = long, y = lat, group = group), colour = "gray", fill = "gray") +
coord_cartesian(ylim = c(30, 52), xlim = c(-128, -114)) +
geom_point(data = te, aes(x = lng, y = lat), color = "blue", size = 5, alpha = 0.6)
print(sp)
This solution uses sp and rgeos packages to manipulate spatial data, the main crux being intersecting lines and a box polygon to get the edge points for arrows. Then if you draw arrows with geom_segment and zero width, the line is invisible and only the arrow head remains.
This function computes the line-box intersections:
boxint <- function(xlim, ylim, xp, yp){
## build box as SpatialPolygons
box = cbind(xlim[c(1,2,2,1,1)],
ylim[c(1,1,2,2,1)])
box <- sp::SpatialPolygons(list(sp::Polygons(list(sp::Polygon(box)),ID=1)))
## get centre of box
x0=mean(xlim)
y0=mean(ylim)
## construct line segments to points
sl = sp::SpatialLines(
lapply(1:length(xp),
function(i){
sp::Lines(list(sp::Line(cbind(c(x0,xp[i]),c(y0,yp[i])))),ID=i)
}
)
)
## intersect lines segments with boxes to make points
pts = rgeos::gIntersection(sl, as(box, "SpatialLines"))
as.data.frame(sp::coordinates(pts), row.names=1:length(xp))
}
And this returns the geom with arrows:
wherelse <- function(xlim, ylim, points){
## get points outside bounding box
outsides = points[!(
points$lng>=xlim[1] &
points$lng <= xlim[2] &
points$lat >= ylim[1] &
points$lat <= ylim[2]),]
npts = nrow(outsides)
## get centre point of box
x = rep(mean(xlim),npts)
y = rep(mean(ylim),npts)
## compute box-point intersections
pts = boxint(xlim, ylim, outsides$lng, outsides$lat)
pts$x0=x
pts$y0=y
## create arrow segments as invisible lines with visible arrowheads
ggplot2::geom_segment(data=pts, aes(x=x0,y=y0,xend=x,yend=y),
lwd=0, arrow=grid::arrow(length=unit(0.5,"cm"),
type="closed"),col="magenta")
}
So your example, the basic plot is:
sp = ggplot() +
geom_polygon(
data=all_states,
aes(x=long, y=lat, group = group),colour="gray",fill="gray" ) +
coord_cartesian(ylim=c(30, 52), xlim=c(-128,-114)) +
geom_point(data=te,aes(x=lng,y=lat),color="blue",size=5,alpha=0.6)
and then add the arrows with:
sp + wherelse(c(-128,-114), c(30,52), te)
Not sure if there's an option to draw arrows exactly like you want them though!
Here is my attempt. This is the closest I got. I used gcIntermediate() for calculating the shortest distance between the center point of your US map and the data points which stay outside of the bbox. Hence, the arrow positions may not be something you want. My hope is that somebody else would deliver a better solution based on this attempt.
I first arranged your df (i.e., te) with the center point in the US zoomed map. I then chose data points which are not in the bbox of the US map. Then, add two columns to indicate the center point of the US map. Rename two columns and calculate the shortest distance with gcIntermediate.
library(dplyr)
library(ggplot2)
library(geosphere)
filter(te, !between(lng, -128, -114) | !between(lat, 30, 52)) %>%
mutate(start_long = (-128 - 114) / 2,
start_lat = (30 + 52) / 2) %>%
rename(end_lat = lat, end_long = lng) %>%
do(fortify(as(gcIntermediate(.[,c("start_long", "start_lat")],
.[,c("end_long", "end_lat")],
100,
breakAtDateLine = FALSE,
addStartEnd = TRUE,
sp = TRUE), "SpatialLinesDataFrame"))) -> foo
foo contains 100 data points to draw respective line. I chose data points which stay close to the bbox boundary. I was specifically looking for two data points for each line so that I could use geom_segment() later. I admit that I played with the filter condition a bit. In the end, I did not subset data using lat in this case.
filter(foo, between(long, -128, -126.5) | between(long, -115.5, -114)) %>%
group_by(group) %>%
slice(c(1,n())) -> mydf
In the next step, I rearranged the data frame based on this link
mutate(mydf, end_long = lag(long), end_lat = lag(lat)) %>%
slice(n()) -> mydf2
Finally I drew the map with arrows. I hope this will provide some kind of base for you. I also hope that other SO users will provide better solutions.
ggplot() +
geom_polygon(data = all_states, aes(x = long, y = lat, group = group),
colour = "gray", fill = "gray" ) +
coord_cartesian(ylim = c(30, 52), xlim = c(-128,-114)) +
geom_point(data = te, aes(x = lng,y = lat), color = "blue", size = 5,alpha = 0.6) +
geom_segment(data = mydf2, aes(x = end_long, xend = long,
y = end_lat, yend = lat, group = group),
arrow = arrow(length = unit(0.2, "cm"), ends = "last"))

Resources