r ggmap - add annotation superimposed on map - r

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

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

ggmap and plot shows different zone on map

With help of ggmap and plot I want to show the centers of states on the map. The result should be something like this
I tried this block of code but is doesnt show above map
data(state)
cen_df <- as.data.frame(state.center)
library(ggmap)
library(ggplot2)
d <- data.frame(lat = cen_df[2],
lon = cen_df[1])
US <- get_map("united states", zoom = 12)
p <- ggmap(US)
p + geom_point(data = d, aes(x = lon, y = lat), color = "red", size = 30, alpha = 0.5)
ggplot_build(p)
But it shows something lie this:
Any help?
I modified your code as follows. The zoom should be 4. It is also better to use base_layer argument to put your ggplot2 object.
data(state)
library(ggmap)
library(ggplot2)
d <- data.frame(lat = state.center$y,
lon = state.center$x)
US <- get_map("united states", zoom = 4)
p <- ggmap(US, base_layer = ggplot(data = d)) +
geom_point(aes(x = lon, y = lat), color = "red", size = 2, alpha = 0.5)
p

Improving a location visibility using ggmap function in R

I am marking some points on a UAE map. I am using ggmap and ggplot2 packages for the purpose.
my code for plotting and marking the map is:
library(ggmap)
library(ggplot2)
d <- data.frame(lat=c(24.534505, 24.529291,24.529291, 24.543425, 24.551134, 24.555446, 24.560406, 24.558412, 24.558670, 24.548625, 24.547120, 24.540850, 24.540428, 24.534505),lon=c(55.415467, 55.419130, 55.432415, 55.465657, 55.462406, 55.473639, 55.471087, 55.465286, 55.465211, 55.438385, 55.439173, 55.427146, 55.427119, 55.415467))
map <- get_map("Sweihan, United Arab Emirates", zoom = 11, maptype = "satellite", source = "google")
Sweihan <- ggmap(map)+geom_point(data = d, aes(x = lon, y = lat))+geom_path(data = d, aes(x = lon, y = lat))+scale_x_continuous(limits = c(55.41, 55.48)) +
scale_y_continuous(limits = c(24.52, 24.5655))
Sweihan
Now the thing is after running this code, I am being able to get correct map and exact points that I wanted to plot. But I am not being about to get the clarity of map. I have tried zoom in get_map function, that isn't helping. Is there any approach that I can use within my code to get a visible map.
Any help will be appreciated!
Thanks in Advance!
Finding the map by address is convenient, but there is more control if you use the longitude/latitude
map <- get_map(location = c(mean(d$lon), mean(d$lat)), zoom = 13, maptype = "satellite", source = "google")
ggmap(map)+
geom_point(data = d, aes(x = lon, y = lat))+
geom_path(data = d, aes(x = lon, y = lat))
ggmap has already set the x and y scales, so you will get warnings if you try to replace them.

Plot and facet a map in 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)

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