Improving a location visibility using ggmap function in R - 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.

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

Shared Boundaries too Dark using geom_path()

I'm working on various mapping of data. One of the maps of interest is taking boundary maps and overlaying them on an exist map. These existing maps may come from Google or other sources.
The issue that I'm having is that when I use geom_path(), with an alpha < 1, if two areas share a boundary, that boundary is darker than other boundaries. Any way to make all the boundaries the same transparency?
library(ggplot2)
library(ggmap)
library(maptools)
library(plyr)
zips <- readShapePoly('cb_2016_us_zcta510_500k.shp')
test <- fortify(zips)
zips#data$id <- rownames(zips#data)
zips.points <- fortify(zips)
zips.df <- join(zips.points, zips#data, by = "id")
test_zips <- c(64002,64012,64013,64014,64015,64029,64030,64034,64050
,64051,64052,64053,64054,64055,64056,64057,64063,64064,64065,64070
,64075,64080,64081,64082,64083,64086,64108,64109,64110,64111,64112
,64113,64114,64121,64123,64124,64125,64126,64127,64128,64129,64130
,64131,64132,64133,64134,64136,64137,64138,64139,64141,64145,64146
,64147,64148,64149,64170,64171,64179,64191,64198,64999,66119,66160
,66205,66206,66207,66208,66209,66211,66222,66224,66251)
Valid_Zips <- zips.df[zips.df$GEOID10 %in% test_zips,]
mapImage <- get_map(location = c(lon = -94.4, lat = 38.9),
color = "color",
source = "google",
maptype = "toner-2011",
zoom = 10)
ggmap(mapImage) +
geom_path(aes(x = long, y = lat, group = group)
,color="red",data=Valid_Zips,size=1,alpha = .3)

plotly feature with ggmap

I am plotting all latitude longitude values which I receive on a map like this.
The map has the color as per the time duration of halt recorded at that point.
My agonies are the following:
I cannot zoom in and zoom out - Centering the map is very painful - And I have 10*100 such collections to be plotted and evaluated
I can hardly make out the areas where the halt was specific, all I can see is that it was good with no halt but reality is not that
Hence I looked for plotly and to my surprise they don't have the support.
How can I achieve the results? I don't have a tool like tableau
Please suggest if you can help.
Below code is an example of what I am trying to do with ggmap :
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)
map <- get_googlemap(center = c(lon_median, lat_median), zoom = 12, maptype = "roadmap",size = c(600, 300), scale = 2)
ggmap(map) +
geom_path(data = lat_long, aes(), size = 2, lineend = "butt") +
geom_point(data = lat_long, color = "red3", size = 1)

Plotting locations of a state in India in maps using R

I have a list of locations from Uttarakhand state in India. The dataset is like this
address lat long
Ajabpur 30.0431765 78.8255226
UHC Ajabpur 30.0431765 78.8255226
Asan Bag 30.5829083 77.7523608
Ashtad 30.6865869 77.8453043
Badripur 30.2843949 78.0656264
Balawala 30.2661801 78.1062092
Ballupur 30.3335862 78.0115041
Barontha 30.066753 79.0192997
I wish to plot these points on the map of Uttarakhand using R. How do i accomplish it?
There are several examples.
but here is one using ggplot2 and ggmap. Look at the manual of ggmap and you will find many options.
data_table <- read.table(text="
add lat long
Ajabpur 30.0431765 78.8255226
UHCAjabpur 30.0431765 78.8255226
AsanBag 30.5829083 77.7523608
Ashtad 30.6865869 77.8453043
Badripur 30.2843949 78.0656264
Balawala 30.2661801 78.1062092
Ballupur 30.3335862 78.0115041
Barontha 30.066753 79.0192997",
header = TRUE)
library(ggplot2)
library(ggmap)
map <- get_map(
location = c(78.825, 30.04),
source = "google", zoom = 15, maptype = "satellite"
)
ggmap(map) +
geom_point(data=data_table, aes(x = long, y = lat), alpha = .5, color="red")

Overlaying points on a ggmap

So I'm putting trees in forest plots into a map of Madagascar
The beginning on the data set looks like this:
#ggmap!
library(ggmap)
library(mapproj)
map <- get_map(location = 'Madagascar', zoom = 4)
ggmap(map)
map <- get_map(location = 'Madagascar', zoom = 10)
geocode("kianjavato")
#lon lat
#47.86682 -21.38024
k <- "kianjavato"
#qmap(k, zoom = 16)
#qmap(k, zoom = 16, source = "stamen", maptype = "toner")
myMap <- get_map(location=k, source="stamen", maptype="toner", crop=FALSE, zoom=16)
# having trouble zooming
ggmap(myMap)
Here is the code I use to make the map and get the points:
ggplot(data=GPScorrect,aes(x=Lon,y=Lat))+geom_point()
But when I go to plot the two together using this code:
ggmap(myMap) + geom_point(data=GPScorrect,aes(x = 'Lon', y ='Lat'))
I get this error message:
Error: Discrete value supplied to continuous scale
PLEASE HELP
When asking questions, be sure to provide a working example that executes front to back until it encounters the error you're asking about. In this case, you don't supply data for GPScorrect.
Un-quote your aesthetic mappings. If you re-work the following stripped down example you should get what you're looking for:
library(ggmap)
library(mapproj)
myMap <- get_map(location = 'Madagascar', zoom = 6)
e1 <- geocode('Antananarivo')
e2 <- geocode('Toamasina')
e3 <- geocode('Antsirabe')
GPScorrect <- rbind(e1, e2, e3)
ggmap(myMap) + geom_point(data=GPScorrect,aes(x = lon, y =lat), color = 'firebrick', size = 3)

Resources