Mapping a csv file in R - r

I'm trying to map crime incidents in Boston. I converted the lat/long to simple feature points but when I plotted it, I only got two points. Does anyone know how to remedy this?
crimedata = read.csv("2019 Crime Incidents.csv", stringsAsFactors = FALSE)
points = st_as_sf(crimedata, coords = c("Lat", "Long"), crs = 4326)
plot(points$geometry, pch=16, col="navy")

You need to plot the points on top of a map:
library(tidyverse)
library(ggmap)
points <- tibble(
lon = c(-100, -90),
lat = c(40, 40),
value = c("A", "B")
)
c(left = -125, bottom = 25.75, right = -67, top = 49) %>%
get_stamenmap(zoom = 5, maptype = "toner-lite") %>%
ggmap() +
geom_point(
data = points,
mapping = aes(color = value),
size = 7
)

Related

how to add labels on my df to replicate on my map

I am using ggplot2 and ggmpap for plotting my co-ordinates in google map and i am not sure how to add labels for each of my coordinates.
I am using following code to plot my map
# loading the required packages
library(ggplot2)
library(ggmap)
# creating a sample data.frame with your lat/lon points
lon <- c(141.98, 141.97, 141.87, 142.05, 142.37, 142.41, 142.16, 141.99)
lat <- c(10.86, 10.99, 11.60, 11.04, 11.13, 11.63, 11.16, 11.38)
df <- as.data.frame(cbind(lon,lat))
# getting the map
register_google(key = "mykey", write = TRUE)
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom =8,
maptype = "hybrid", scale = 2)`
# plotting the map with some points on it
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, alpha = 0.2), color = "yellow", fill = "pink", size = 4, shape = 10) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)
i got the map too but now i need to add labells for each of my cordinates. could someone help me in the script

How to align ggmap CRS of google map longitude latitude

Despite the many posts of CRS projections, etc I cannot stop my town from sinking.
If I go to Google Maps and enter -35.016, 117.878 the town of Albany is on dry land (as per following image):
If I enter the lat/long into R and try to map using the simple features package and ggmap, the town is in the ocean:
library(tidyverse)
library(sf)
library(lwgeom)
library(ggmap)
lat <- c(-35.016)
lon <- c(117.878)
df <- tibble(lon,lat) %>% st_as_sf( coords = c("lon", "lat"), crs = 4326)
bbox_aus <- c(left = 113.338953078, bottom = -43.6345972634, right = 153.569469029, top = -10.6681857235)
ggmap_aus <- ggmap(get_stamenmap(bbox_aus, zoom = 5, maptype = "toner-background"))
ggmap_aus +
geom_sf(data = df, colour = "red" , size = 3, alpha = 0.5, inherit.aes = FALSE) +
# coord_sf(datum = sf::st_crs(4326)) +
labs(title = "Albany Sinking",
x = NULL,
y = NULL) +
theme_bw()
It works if you use geom_point() with the lon and lat as x and y.
df <- tibble(lon,lat) %>% st_as_sf( coords = c("lon", "lat"), crs = 4326,
remove = FALSE)
ggmap_aus +
geom_point(data = df, colour = "red", size = 3, alpha = 0.5,
aes(x = lon, y = lat)) +
# coord_sf(datum = sf::st_crs(4326)) +
labs(title = "Albany is saved",
x = NULL,
y = NULL) +
theme_bw()
Based on this comment, using geom_point() with an x and y aesthetic aligns more closely with how ggmap produces a ggplot.
Unfortunately, I'm not sure how to make it work with geom_sf(), which plots using the geometry column. There's some discussion in that linked comment, but the solution seems to be to use inherit.aes = FALSE, which you've already tried.
Based on the warning Coordinate system already present. Adding new coordinate system, which will replace the existing one., I assume that the ggmap object has some coordinate system that is not 4326, but I couldn't find how to access it. I did try reprojecting df to EPSG: 3857, but that did not work.

Mark the center of state in stamen maps

I want to draw a map like the below picture of untitled state and show the center of all state by a colorful circle.
There is google map API which can use in R.But it seems that it's no longer available to use free of charge.
How can I draw this picture by Stamn Maps library in R?
If there is a good tutorial about Stamn Maps, I'll appreciate any helps.
thanks for your answers I find one of the solutions that shows map in r by Stamn Maps
d <- data.frame(lat = state.center$y,
lon = state.center$x)
#-128.5, 27.5, -69, 49
US <- get_stamenmap(bbox = c(left = -128.5, bottom = 27.5, right =
-68, top = 50) ,zoom = 4, maptype = c("terrain",
"terrain-background", "terrain-labels", "terrain-lines", "toner",
"toner-2010", "toner-2011", "toner-background", "toner-hybrid",
"toner-labels", "toner-lines", "toner-lite", "watercolor"),
crop = TRUE, messaging = FALSE, urlonly = FALSE,
color = c("color", "bw"), force = FALSE, where = tempdir())
p <- ggmap(US, base_layer = ggplot(data = d)) +
geom_point(aes(x = lon, y = lat), color = "blue", size = 2, alpha = 0.5)
p
A minimal example to kickstart your journey:
set.seed(1702)
points <- data.frame(lon = rnorm(10, -95.4, 0.1),
lat = rnorm(10, 29.7, 0.1))
# get_stamenmap() defaults to the map of Houston, TX if there
# is no boundary box defined in the form of:
# c(lon_min, lat_min, lon_max, lat_max)
# For more information see ?get_stamenmap
ggmap(get_stamenmap()) +
geom_point(data = points,
aes(lon, lat),
color = "red")

Plot circle with a certain radius around point on a map in ggplot2

I have a map with the 8 points plotted on it:
library(ggplot2)
library(ggmap)
data = data.frame(
ID = as.numeric(c(1:8)),
longitude = as.numeric(c(-63.27462, -63.26499, -63.25658, -63.2519, -63.2311, -63.2175, -63.23623, -63.25958)),
latitude = as.numeric(c(17.6328, 17.64614, 17.64755, 17.64632, 17.64888, 17.63113, 17.61252, 17.62463))
)
island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 13, maptype = "satellite")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, color = "#ff0000")
islandMap + RL + scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) + scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0))
Now I want to plot a circle around each of the 8 plotted locations. The circle has to have a radius of 450 meters.
This is what I mean, but then using ggplot: https://gis.stackexchange.com/questions/119736/ggmap-create-circle-symbol-where-radius-represents-distance-miles-or-km
How can I achieve this?
If you only work on a small area of the earth, here is a approximation. Each degree of the latitude represents 40075 / 360 kilometers. Each degrees of longitude represents (40075 / 360) * cos(latitude) kilomemters. With this, we can calculate approximately a data frame including all points on circles, knowing the circle centers and radius.
library(ggplot2)
library(ggmap)
data = data.frame(
ID = as.numeric(c(1:8)),
longitude = as.numeric(c(-63.27462, -63.26499, -63.25658, -63.2519, -63.2311, -63.2175, -63.23623, -63.25958)),
latitude = as.numeric(c(17.6328, 17.64614, 17.64755, 17.64632, 17.64888, 17.63113, 17.61252, 17.62463))
)
#################################################################################
# create circles data frame from the centers data frame
make_circles <- function(centers, radius, nPoints = 100){
# centers: the data frame of centers with ID
# radius: radius measured in kilometer
#
meanLat <- mean(centers$latitude)
# length per longitude changes with lattitude, so need correction
radiusLon <- radius /111 / cos(meanLat/57.3)
radiusLat <- radius / 111
circleDF <- data.frame(ID = rep(centers$ID, each = nPoints))
angle <- seq(0,2*pi,length.out = nPoints)
circleDF$lon <- unlist(lapply(centers$longitude, function(x) x + radiusLon * cos(angle)))
circleDF$lat <- unlist(lapply(centers$latitude, function(x) x + radiusLat * sin(angle)))
return(circleDF)
}
# here is the data frame for all circles
myCircles <- make_circles(data, 0.45)
##################################################################################
island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 13, maptype = "satellite")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, color = "#ff0000")
islandMap + RL +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0)) +
########### add circles
geom_polygon(data = myCircles, aes(lon, lat, group = ID), color = "red", alpha = 0)
Well, as the referred posting already suggests - switch to a projection that is based in meters, and then back:
library(rgeos)
library(sp)
d <- SpatialPointsDataFrame(coords = data[, -1],
data = data,
proj4string = CRS("+init=epsg:4326"))
d_mrc <- spTransform(d, CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=#null +no_defs"))
Now, the width can be specified in meters:
d_mrc_bff_mrc <- gBuffer(d_mrc, byid = TRUE, width = 450)
Transform it back and add it to the plot using geom_path:
d_mrc_bff <- spTransform(d_mrc_bff_mrc, CRS("+init=epsg:4326"))
d_mrc_bff_fort <- fortify(d_mrc_bff)
islandMap +
RL +
geom_path(data=d_mrc_bff_fort, aes(long, lat, group=group), color="red") +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0))
Calculating distance in km given latitude and longitude isn't super straightforward; 1 degree lat/long is a greater distance at the equator than at the poles, for example. If you want an easy workaround that you can eyeball for accuracy, you might try:
islandMap + RL +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0)) +
geom_point(aes(x = longitude, y = latitude), data = data, size = 20, shape = 1, color = "#ff0000")
You'll need to adjust the size paramter in the 2nd geom_point to get closer to what you want. I hope that helps!
An accurate solution is using the geosphere::destPoint() function. This works without switching projections.
Define function to determine 360 points with a certain radius around one point:
library(dplyr)
library(geosphere)
fn_circle <- function(id1, lon1, lat1, radius){
data.frame(ID = id1, degree = 1:360) %>%
rowwise() %>%
mutate(lon = destPoint(c(lon1, lat1), degree, radius)[1]) %>%
mutate(lat = destPoint(c(lon1, lat1), degree, radius)[2])
}
Apply function to each row of data and convert to data.frame:
circle <- apply(data, 1, function(x) fn_circle(x[1], x[2], x[3], 450))
circle <- do.call(rbind, circle)
Then the map can be easily obtained by:
islandMap +
RL +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0)) +
geom_polygon(data = circle, aes(lon, lat, group = ID), color = "red", alpha = 0)
A solution using st_buffer() from the sf package.
library(ggmap)
library(ggplot2)
library(sf)
data <- data.frame(
ID = 1:8,
longitude = c(-63.27462, -63.26499, -63.25658, -63.2519,
-63.2311, -63.2175, -63.23623, -63.25958),
latitude = c(17.6328, 17.64614, 17.64755, 17.64632,
17.64888, 17.63113, 17.61252, 17.62463)
)
Convert data.frame to sf object:
points_sf <- sf::st_as_sf(data, coords = c("longitude", "latitude"), crs = 4326)
For this example we use UTM zone 20, which contains the coordinates of the island:
data_sf_utm <- sf::st_transform(points_sf, "+proj=utm +zone=20")
Now we can buffer the point by 450 meters:
circle <- sf::st_buffer(data_sf_utm, dist = 450)
ggmap seems to have some issues with geom_sf. Setting inherit.aes to FALSE returns the desired map.
island <- ggmap::get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 14, maptype = "satellite")
ggmap(island, extent = "panel", legend = "bottomright") +
geom_sf(data = points_sf, color = "red", inherit.aes = FALSE) +
geom_sf(data = circle, color = "red", alpha = 0, inherit.aes = FALSE)
Created on 2020-10-11 by the reprex package (v0.3.0)

ggmap extended zoom or boundaries

I am trying to fix the following problem.
I use ggplot2 to plot a map of an island:
island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 14, maptype = "satellite")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, size = 4, color = "#ff0000")
islandMap + RL
Coordinates of the RL points:
data = data.frame(
ID = as.numeric(c(1:8)),
longitude = as.numeric(c(-63.27462, -63.26499, -63.25658, -63.2519, -63.2311, -63.2175, -63.23623, -63.25958)),
latitude = as.numeric(c(17.6328, 17.64614, 17.64755, 17.64632, 17.64888, 17.63113, 17.61252, 17.62463))
)
Now the problem is that when I use zoom = 13 the island is too small in the plot and when I use zoom = 14 it is perfectly centered. But when I plot the RL points, two get cut off because its too much to the East and the other one too much to the West. I looked some solutions up like the following one, using a boundary box. However, I am bound to using satellite imagery, so bound to Google, which doesn't support the boundary box solution.
lon = data$longitude
lat = data$latitude
box = make_bbox(lon, lat, f = 0.1)
island = get_map(location = box, zoom = 14, source = "osm")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, size = 4, color = "#ff0000")
islandMap + RL
How can I make sure that the map is as big as using zoom = 14, all the points are within the plot (plus a margin around this) and satellite imagery?
Using my answer from this question, I did the following. You may want to get a map with zoom = 13, and then you want to trim the map with scale_x_continuous() and scale_y_continuous().
library(ggmap)
library(ggplot2)
island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 13, maptype = "satellite")
RL <- read.table(text = "1 17.6328 -63.27462
2 17.64614 -63.26499
3 17.64755 -63.25658
4 17.64632 -63.2519
5 17.64888 -63.2311
6 17.63113 -63.2175
7 17.61252 -63.23623
8 17.62463 -63.25958", header = F)
RL <- setNames(RL, c("ID", "Latitude", "Longitude"))
ggmap(island, extent = "panel", legend = "bottomright") +
geom_point(aes(x = Longitude, y = Latitude), data = RL, size = 4, color = "#ff0000") +
scale_x_continuous(limits = c(-63.280, -63.20), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.60, 17.66), expand = c(0, 0))

Resources