How to get only street labels with get_googlemap - r

I´m trying to get only the street labels from Google Maps API using get_googlemap()from the ggmap package. I read Google Static Maps Developer and also this and managed to get rid of the labels of the adminsitrative feature by specifying style = "feature:administrative|visibility:off"
cntr <- c(-71.445, -33.055)
map <- get_googlemap(location = "Chile", center = cntr,
zoom = 13, source = "google",
maptype ="roadmap", color = "bw",
style = "feature:administrative|visibility:off")
mapplot <- ggmap(map)
But I´m still struggling to just get the map with the street labels. Is there a way to do it? or get_googlemap doesn't support that option? I have tried with style = "feature:landscape|visibility:off" but nothing changes, when I use style = "feature:landscape|element:geometry|visibility:off" I get this:

Related

Locking the zoom in on Mapview?

I am creating a map plotted with points around the UK map with map view library in R. I want to have a fixed zoom or zoom button disabled. I don't want a Street view/Zoom in due to GDPR issues. I wonder if there is a solution for this? Thanks.
library(sf)
library(mapview)
mapview(datamap, xcol = "Longitude", ycol = "Latitude")
This is not possible with mapview and kinda defeats the purpose of the package. If you want this type of behaviour you either need to create the complete map using leaflet or at least create the base map with leaflet and then use it with mapview:
library(mapview)
library(leaflet)
map = leaflet(options = leafletOptions(minZoom = 12, maxZoom = 12)) %>%
addTiles()
mapview(franconia, map = map)

Mapview Popup Graph Appear on Hover?

Is there a way to make popup graphs appear on hover (rather than click) in Mapview? Alternatively, is it possible for the graphs to appear open by default? Rather than produce my own reproducible example, I would defer to the example given with the R Mapview documentation.
I am fairly new to R and Mapview so any guidance is greatly appreciated!
I've just pushed an update to package leafpop which provides the popup functionality used in mapview. This should provide what you want (at least partly - as mapview() will still need to be updated). This allows you to now specify tooltip = TRUE in addPopupImages (in addPopupGraphs via ...). Note that it is encouraged to use addPopup* functions over the classic popup* functions because they also work in non-interactive setting, e.g. when saving a map locally.
library(sf)
library(leaflet)
library(lattice)
library(leafpop)
pt = data.frame(x = 174.764474, y = -36.877245)
pt = st_as_sf(pt, coords = c("x", "y"), crs = 4326)
p2 = levelplot(t(volcano), col.regions = terrain.colors(100))
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = pt, group = "pt") %>%
addPopupGraphs(
list(p2)
, group = "pt"
, width = 300
, height = 400
, tooltip = TRUE
)
Not sure when and how to integrate this into mapview() as this is a bit more complicated than the classic popup* functions (because we need to know something about the map object we create with mapview before we create it...). In any case, I hope this is at least partly useful and helps resolve your issue.

How to use a custom marker symbol in plotly scattermapbox for R

I am currently developing a small shiny application that uses an interactive map. The idea is very simple: I am trying to plot a handful of points in a map using lat/long coordinates. I cannot use google's API, so I used plotly's scatter plot, specifying a map layout.
This is a generic representation of what I have so far:
fig <- plotly::plot_ly(
data = data,
lat = data$lat,
lon = data$long,
mode = "markers",
marker = list(size = 10),
type = 'scattermapbox',
hovertext = paste0(
paste0("Some info 1: ", data$field1, "\n"),
paste0("Some info 2: ", data$field2, "\n"),
paste0("Some info 3: ", data$field3)
)
)
# specify layout as open street map
fig <- fig %>%
layout(
mapbox = list(
style = 'open-street-map',
zoom = 3,
center = list(lat = -20.72623, lon = -47.74942))
)
print(fig)
Considering that my "data" dataset has the fields and coordinates, the result I'm getting is this:
What I am not able to do is change the marker symbols. According to plotly's documentation (https://plotly.com/r/reference/#scatter-mode), it is as simple as defining symbol = "some_symbol", but that won't work. I've seen some examples and some workarounds, but none seem to work very intuitively.
Ideally, I wanted to generate a map that looked like this:
Does anyone know if this is even possible with a simple scatterplot? Is there any other way? Am I missing something here?
I found a solution for this, although using a different package.
Using the leaflet package, I was able to create a map using the same open-street-map layout. The default marker symbol/icon is a pin, but you can create any customized icon you want, using svg files. The leaflet documentation is specified
here.
My specific solution was defining the hovertext as a set of labels and then simply calling the addMarkers function inside the leaflet object.
labels <- lapply(seq(nrow(data)), function(i) {
paste0( '<p><b> Some information: ', data[i, "field1"], '</b></p>',
"<p> Some other information : ", data[i, "field2"] / 1000, ' MWm </p>',
"<p> Some different information : ", data[i, "field3"],'</p>')
})
fig <- leaflet::leaflet(data = data) %>% leaflet::addTiles() %>%
leaflet::addMarkers(
~as.numeric(longitude),
~as.numeric(latitude),
label = lapply(labels, htmltools::HTML)
)
print(fig)

ggmap and Google Earth not giving same position (R)

I'm currently trying to plot the position of some weather stations in Switzerland. Using pretty standard code with ggmap and geom_point.
register_google(key = "YOUR KEY")
map <- get_map(location='Bern', zoom=12, maptype = "terrain", color = "color")
ggmap(map) + geom_point( aes(x=stations_coord$long, y=stations_coord$lat)
, data = stations_coord, colour = "red", size = 3 )
Problem: All values are misplaced in the plot when comparing to Google Earth
Here is an example for the Bern station.
I really don't understand how this is possible. Also there are no threads on this issue anywhere.
Could anyone help me?
The satellite map and terrain maps use different datums. You are comparing a satellite image with a terrain map - those are different maps and are likely to use different datums. The default datum for google earth (the screencap of your web browser) is WGS84. I don't know what the default datum for the terrain map is but it appears to be different.
Try changing 'maptype = "satellite"' and you will get the same lat/long:
map <- get_map(location='Bern', zoom=12, maptype = "satellite", color = "color")
ggmap(map) + geom_point( aes(x=stations_coord$long, y=stations_coord$lat)
, data = stations_coord, colour = "red", size = 3 )
Futher reading on datums: https://en.wikipedia.org/wiki/Geodetic_datum
A side note: accidents happen all the time because of this. Ships reading GPS are using one datum and their charts (maps) are in another datum, so they aren't where they think they are and can crash into stuff. This can also happen with aircraft and in land navigation.
Bottom line: ggmap uses the datums associated with the map that you tell it to look at. You are telling ggmap to look at one type of map (terrain) and comparing it with another type of map (satellite) - probably different datums.

How to plot polylines in multiple colors in R?

I'm working on a custom route planner in R at the moment. I'm using the output of the Google Maps Directions API. I want to show the route on a map between two places. Everything is going great so far. The only problem is that I don't know how to give the route multiple colors based on Speed at the moment. I searched the internet for a few days and I couldn't find something that fits my goal. That's why I made this post.
Then I visualized it in Leafet with te following code:
#install.packages("leaflet")
library(leaflet)
pal <- colorNumeric(
palette = unique(polyline$Col),
domain = polyline$Speed,
na.color = "#FFFFFF"
)
rm(map)
map <- leaflet()
map <- addTiles(map)
a <- 1
for(a in length(unique(polyline$Step_ID))){
map <- addPolylines(map,lng = polyline$Lon,
lat = polyline$Lat,
data = polyline[polyline$Step_ID==a,],
color = polyline$col)
a <- a + 1
}
map <- addLegend(map,"bottomright", pal = pal, values = polyline$Speed,
title = "Speed",
opacity = 1)
map
So far I think you have to create multiple PolyLines(correct me if I'm wrong) to plot multiple colors in the route. That's why I made a for loop, to add ever PolyLine into the map.
Everthing is just how want it. The only problem is the coloring of the line. I want the coloring of the lines just like Google does with traffic.
Can someone help me out with this please?
To fully replicate your question you need to provide us with the actual data for polyline (i.e, not a screenshot). Until then, I'm going to create my own data set and show you how to create the coloured lines.
And, as you're using Google's API to get the directions, I'm assuming you'll have an API key, so I'm going to show you how to do it using my googleway package
library(googleway)
api_key <- "your_api_key"
directions <- google_directions(origin = "St Kilda, Melbourne, Australia",
destination = "Geelong, Victoria, Australia",
key = api_key)
## the results of the API give you distance in metres, and time in seconds
## so we need to calculate teh speed
spd <- (directions$routes$legs[[1]]$steps[[1]]$distance$value / 1000) / (directions$routes$legs[[1]]$steps[[1]]$duration$value/ 60 / 60)
## then we can start to build the object to use in the plot
## and as we are staying within Google's API, we can use the encoded polyline to plot the routes
## rather than extracting the coordinates
df <- data.frame(speed = spd,
polyline = directions$routes$legs[[1]]$steps[[1]]$polyline)
df$floorSpeed <- floor(df$speed)
colours <- seq(1, floor(max(df$speed)))
colours <- colorRampPalette(c("red", "yellow","green"))(length(colours))
df <- merge(df,
data.frame(speed = 1:length(colours),
colour = colours),
by.x = "floorSpeed",
by.y = "speed")
map_key <- "your_map_api_key"
google_map(key = map_key) %>%
add_polylines(data = df, polyline = "points", stroke_colour = "colour",
stroke_weight = 5, stroke_opacity = 0.9)
See this answer for a way of making the route planner in Shiny.

Resources