I have a very small SpatialLinesDataFrame which I need to plot with Leaflet in R.
Unfortunately, for some reason I'm not able to do so.
This is the data (it's 12KB).
I try to do:
library(leaflet)
load("mylines.Rdata")
leaflet() %>% addTiles() %>% addPolylines(data=mylines)
However the resulting map does not make sense, I can only see a line in the top of the screen and this is not what should be plotted.
This is the result:
Instead, If I do:
library(mapview)
mapview(mylines)
Result:
It works perfectly, despite mapview using leaflet underneath.
What am I doing wrong in the Leaflet syntax?
I am used to working with leaflet providing rasters, so I usually use the addRasterImage function, which needs data projected over leaflet's display projection (EPSG:3857). However, for polygons and lines, as #TimSalabim correctly pointed out, this is not the case.
So the solution in this case was to just not reproject the data to the leaflet projection beforehand, and provide it in lat-lon coordinates (EPSG:4326).
mapview worked since it does this automagically.
Related
I am trying to plot a world map, where the colors correspond to a specific variable. However, it does not show small states like "Andorra" or "Liechtenstein", even though they are in the data frame (see df$COUNTRY). I think the problem is, that the borders are not drawn with much detail. Hence one can not see the small states. Is there a way draw the borders with more detail like with the packs, maps, and mapdata (e.g wolrd_detailed <- map_data ("worldHires")? Or any other way to make the small states visible? Here is the example from the plotly website. It does not show small states:
library(plotly)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv")
fig <- plot_ly(df, type='choropleth', locations=df$CODE, z=df$GDP..BILLIONS., text=df$COUNTRY, colorscale="Blues")
fig
Thanks for the help!
I'm rather new to creating maps and stuff in R, so please bear with me.
I'm trying to plot a building outline in R using data from OpenStreetMap, instead of having to define a polygon myself for plotting the outline of the building. But I cannot get my head around it.
The example below is what I have done so far to get the location:
library(leaflet)
leaflet() %>%
setView(lng=10.153523, lat=57.207181, zoom = 16) %>%
addTiles() %>%
addMarkers(lng=10.153523, lat=57.207181, popup="My building example")
And the example building data is available on https://www.openstreetmap.org/way/481541280
How do I integrate this building outline data into my R script? Is there a neat way of doing this other than extracting the points one by one and making a polygon using leaflet in R?
I'm using R, and I want to overlay some raster data (e.g. a temperature map from a model) over an interactive map which allows panning and zooming. Ideally, I'd like to overlay over Google Maps or OpenStreetMaps. The input data can be in shapefiles, KML, raster data or whatever comes in handy.
I know I can easily do this non-interactively using either googleVis, ggmap or RgoogleMaps. But I do not want to use tiles, I want interaction! Zooming, panning etc., directly from the browser.
googleVis, as far as I know, unfortunately only allows to show interactively points or addresses, not areas.
This question is very similar but I definitely want to try to do this using R. I can create the KML or geoJSON from R, but how do I overlay it from R directly?
OpenStreetMaps is also fine, however I've not found any reference on how to overlay data over it from R, despite the fact that OSM seems to have a pretty straightforward API.
The mapview package has been developed for this particular purpose. It also comes with a variety of background map layers. For a short introduction to what mapview is capable of, feel free to browse the package vignette. Here, for example, is some code that displays locations of selected breweries in Franconian Switzerland overlaid by a sample Landsat 8 scene (band 10). Check out ?breweries91 and ?poppendorf to retrieve information about the data used below and ?mapview to grow familiar with the numerous costumization options.
## require package
# install.packages("mapview")
library(mapview)
## visualize breweries and add landsat 8 band 10
mapview(breweries91) +
poppendorf[[10]]
The leaflet package may be of interest for you. You can easily add a raster object. From the documentation
Two-dimensional RasterLayer objects (from the raster package) can be
turned into images and added to Leaflet maps using the addRasterImage
function.
And here is an example also from the documentation:
library(leaflet)
library(raster)
r <- raster("nc/oisst-sst.nc")
pal <- colorNumeric(c("#0C2C84", "#41B6C4", "#FFFFCC"), values(r),
na.color = "transparent")
leaflet() %>% addTiles() %>%
addRasterImage(r, colors = pal, opacity = 0.8) %>%
addLegend(pal = pal, values = values(r),
title = "Surface temp")
I'm using R 3.1.2, trying to create a choropleth using a world map. The quirk is that, because my audience is in Asia I need to recenter the map. From the documentation it seems like nowrapRecenter() is perfect for this, but I find that it doesn't seem to work as advertised. For example, start without any recentering:
library(maps)
library(maptools)
library(rgdal)
data(wrld_simpl)
plot(wrld_simpl)
Now try to recenter at 148E longitude, in order to move Asia closer to the centre of the map while splitting as few land masses as possible at the left/right margins:
library(maps)
library(maptools)
library(rgdal)
data(wrld_simpl)
world <- nowrapRecenter(wrld_simpl,offset=148,avoidGEOS=TRUE)
plot(world)
What you get is a bit messy. Not only is the map centred at 180E longitude, but there are scratches all across the map where polygons which nowrapRecenter() should have been divided and re-closed at the left/right are extending across the full width of the map. In fact recentering does not appear to work cleanly for any chosen offset.
A similar question came up before, and the final comment there provided an example using nowrapRecenter(), but it no longer seems to work. What's the best way to recenter a world map (using SpatialPolygons) and have the polygons at the left/right margins be properly divided?
Thanks!
This is only a partial answer, so if it's not adequate let me know and I'll delete it.
The problem appears to be that transforming a Mercator projection fails near the poles. If you are willing to exclude Greenland and Antarctica, this works.
library(maptools)
data(wrld_simpl)
wrld <- wrld_simpl[!(wrld_simpl$NAME %in% c("Greenland","Antarctica")),]
library(ggplot2)
ggplot(wrld,aes(x=long,y=lat,group=group))+
geom_polygon(fill="white",color="grey30")+
coord_map(orientation=c(90,0,148))+
scale_x_continuous(breaks=c(0,60,120,180,-120,-60))+
theme_bw()
Even with this restriction, nowrapRecenter(...) failed.
how to plot something like this:
see image here, I didn't have 10 reputation so I can not post image, so paste a image URL here.
http://www.flickr.com/photos/97751721#N07/9089566951
or
http://www.flickr.com/photos/97751721#N07/9091786734
right part of this map is a zoom in map with limitation, that is what I want
if it is use R code or other program language will be more great!
If your have everything as Spatial object you could simply plot with standard plot() and set lim to what ever you like.
require(maptools)
shape <- readShapePoints("shapefile.shp")
plot(shape, xlim=c(minXcoordinate, maxXcoordinate) ylim=c(minYcoordinate, maxYcoordinate))
It is a little unclear to me what exactly you want to do. Do you just want to make a map with a specific set of lon/lat boundaries? Do you need to plot data on top of it? Do you need to control the appearance to make it look like the example you give (with line boundaries & minimal geographic information)?
The ggmap package may get you started on this. The syntax goes like this:
require(ggmap)
# The location argument defines the center of the map
exampleMap <- get_map(location = c(lon = -95, lat = 30), zoom=5)
ggmap(exampleMap)
You can then add data to the map if you like (exactly how is left as an exercise to the motivated student!)