Plot and facet a map in R - r

main data frame
data.frame(lat = c(38.6938, 38.4262, 32.7607, 37.083, 39.4619, 41.0042),
lon = c(-9.20587, -8.90007, -16.9595, -8.90918, -8.38391, -7.9699),
views = c(13565, 27020, 74420, 18550, 73253, 14615),
challenge = c("SPOT CIDADE", "SPOT NATUREZA I",
"SPOT NATUREZA II", "SPOT ROMANCE",
"SPOT PATRIMONIO", "SPOT GASTRONOMIA"),
stringsAsFactors = FALSE)
I am trying to plot a map, for print, and the result would be a map for each challenge and the points changing their size based on the views each video has.
So far, my code brings all points in the same map - i'm having issues with the faceting and changing the size of the points.
When I change the variable in the aesthetics the points get huge.
The zoom levels are either too close or too far.
The code i'm using is below.
every example I see is either too far from want I need, or it just don't
work.
Am I doing it right, or this needs another approach?
Thanks!
library(ggplot2)
library(ggmap)
lon <- as.numeric(new$lon)
lat <- as.numeric(new$lat)
spots_df <- as.data.frame(cbind(lon, lat))
mapa_spots <- get_map(location = c(lon = mean(spots_df$lon), lat = mean(spots_df$lat)), zoom = 6, maptype = "terrain", scale = 2)
plot_spots <- ggmap(mapa_spots) +
geom_point(data = spots_df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 2, shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)
plot_spots
The result I get from this code:

Assuming your data frame is called dt, here is a plot with ggmap and ggplot2. The key is to use base_layer argument with ggplot(dt, aes(...)) so that you can further add other geom just like a ggplot call. You mentioned that you want each challenge in a separate map in your comment. I think facet_wrap is probably what you are looking for.
library(ggplot2)
library(ggmap)
mapa_spots <- get_map(location = c(lon = mean(dt$lon),
lat = mean(dt$lat)),
zoom = 6, maptype = "terrain", scale = 2)
ggmap(mapa_spots, base_layer = ggplot(dt, aes(x = lon, y = lat, size = views))) +
geom_point() +
facet_wrap(~challenge, ncol = 3)
DATA
dt <- data.frame(lat = c(38.6938, 38.4262, 32.7607, 37.083, 39.4619, 41.0042),
lon = c(-9.20587, -8.90007, -16.9595, -8.90918, -8.38391, -7.9699),
views = c(13565, 27020, 74420, 18550, 73253, 14615),
challenge = c("SPOT CIDADE", "SPOT NATUREZA I",
"SPOT NATUREZA II", "SPOT ROMANCE",
"SPOT PATRIMONIO", "SPOT GASTRONOMIA"),
stringsAsFactors = FALSE)

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

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")

r ggmap - add annotation superimposed on map

Questions about map legend editing exist (e.g.), but not exactly what I need.
Using ggmap, how do I select points in a map and add annotations superimposed on the map? Take the following code:
Map <- get_map(location = 'Santiago, Chile', zoom = 6, maptype = "terrain")
Map <- ggmap(Map)
Points <- data.frame(lon=c(-71.82718,-71.31263),lat=c(-34.36935,-34.29322))
Map_Points <- Map + geom_point(data = Points,aes(x=lon,y=lat,size=6))
So now I have a nice map with a few points. How do I write some annotation near one of the points?
Quite straightforward:
Code
library(ggrepel) # for the auto-repelling label
Map +
geom_point(data = Points,
aes(x = lon, y = lat),
size = 3) +
geom_label_repel(data = Points,
aes(x = lon, y = lat, label = name),
size = 3,
vjust = -2,
hjust = 1)
Data
library(tmaptools) # for the geocode lookup
library(ggmap)
santiago_coords <- rbind(as.numeric(paste(geocode_OSM("Santiago, Chile")$coords)))
Map <- get_map(location = santiago_coords, zoom = 6, maptype = "terrain")
Map <- ggmap(Map)
Points <- data.frame(lon=c(-71.82718,-71.31263),
lat=c(-34.36935,-34.29322),
name=c("Location One", "Location Two"))

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))

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)

Resources