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

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

Related

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

Resize output window of ggmap

I am plotting few lat longs using ggmap and I get the output like this
I use the following code to generate this output, below code is part of the o/p
library(ggplot2)
library(data.table)
library(ggmap)
library(maps)
library(mapdata)
lat <- seq(31.26415,31.26831,0.00010)
lon <- seq(76.80890,76.82320,0.00015)
lon <- lon[seq(1,96.2)]
lon <- lon[1:42]
lat_long <- data.frame(lat,lon)
lat_median <- 31.26751
lon_median <- 76.82003
map <- get_map(location = c(lon_median, lat_median), maptype = "roadmap", zoom = 15)
ggmap(map) +
geom_path(data = lat_long, aes(), size = 2, lineend = "butt") +
geom_point(data = lat_long, color = "red3", size = 1)
My output window has lot of white space, which I could have used to show in the map. Can I increase this map output to better fit the window?
If you use the get_googlemap function you can specify the dimensions and it doesn't have to be square e.g.:
map <- get_googlemap('paris', zoom = 15, size = c(500, 200), scale = 2)

Display all latitude and longitude once in a MAP

I have a dataset like
latitude longitude Class prediction
9.7 21.757 244732 1
12.21 36.736 112206 0
-15.966 126.844 133969 1
Now i am trying to group all '1' at prediction column and take their latitude and longitude, later i want to display the all points on a single map.
Actually the code i wrote its takes each '1' on prediction column and takes lat and long respectively and display one point on map each time. But I want to collect all lat and long where prediction is 1 and display all points on a one map.
library(ggplot2)
library(ggmap) #install.packages("ggmap")
#data set name testData1
for (i in 1:100){
if (testData1$prediction[i]==1) {
lat <- testData1$latitude[i]
lon <- testData1$longitude[i]
df <- as.data.frame(cbind(lon,lat))
# getting the map
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4,
maptype = "satellite", scale = 2)
# 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)
}
}
I think you're overcomplicating things. You could simply subset df like so:
ggmap(mapgilbert) +
geom_point(data = subset(df, prediction == 1), aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +
guides(fill = FALSE, alpha = FALSE, size = FALSE)

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