R multiple layers in leaflet (using tmap) - r

I want to create a map using the library tmap (to save loads of bother with shapefiles).
Then I convert into leaflet, with a drop down menu to select which data I want to show.
In the example below, I wish for a select option to show either HPI and pop_est instead of World. I have managed to do this but I have not worked out how to have it show it only shows one by default (i.e HPI)
#Load libraries
library(tmap)
library(leaflet)
#Load Data
data("World")
#Create Tmap object
HPI<-tm_shape(World2,name="HPI") +
tm_polygons("HPI",alpha=0.5)
Pop<-tm_shape(World2,name="Pop") +
tm_polygons("pop_est",alpha=0.5)
#Convert to leaflet
lf<-tmap_leaflet(c(HPI,Pop))
#Print leaflet map
lf

Related

How do I use draw_ext() to choose my own basemap bounds with the basemaps package in R?

I am trying to learn how to properly use the basemap package in R. As practice, I am trying to load the 'WorldImagery' map provided by ESRI and overlay World Magnetic Map .csv file I have created to plot inclination angle isolines. I would like to choose a bounding box to see this at a finer scale using the draw_ext() option in the package, but I can't figure out how to get it to run. I know I could load a basemap with other packages, but I'd like to figure out how the draw_ext() function works specifically. The error message appears when I press "done"
library(basemaps)
library(ggplot2)
draw_ext() #not sure if I choose map here or in the ggplot function
set_defaults(map_service = "esri", map_type = "world_imagery")
ggplot() +
geom_raster(data=WorldMagneticMap2022,aes(x=Longitude,y=Latitude,group=Inclination,fill=Inclination),color = '#00000000',interpolate = T)+
basemap_gglayer(draw_ext()) + #Or if I need to choose map here
scale_fill_identity() +
coord_sf()
The interactive map appears correctly in my Viewer, but then I receive the following error:
Error in attr(ed_record, "recorder") <- attr(ed, "recorder", exact = TRUE) :
attempt to set an attribute on NULL

how to add my own shape file trajLevel() in openair package R

I am using this following code for plotting the trajectories...........
library(openair)
load("GDASNDL1000m.Rdata")
trajLevel(traj,method="hexbin",col="jet",xbin=40,parameters=NULL,
orientation=c(90,0,0),projection="mercator")
result https://github.com/adeckmyn/maps/files/2667752/GDASNDL1000m.zip
Here, I would like to change the base world map with my own shape file.
my shape file is follows....
z1=maptools::readShapePoly("/home/sateeshm/shapefiles/ncmrwf/india_map")
library(maps)
map(z1)
https://github.com/adeckmyn/maps/files/2667336/World-India.zip
#
Now, the actual question is how to link z1 to trajLevel?
To avoid the hard-coded call to "world" in openair, you wil have to create a new world database in the same file based format as that of the "maps" package.
Probably the simplest way to do this, is to use the mapMaker package. This package is not on CRAN but can be found on github. It is the package I used to create the standard world map. The documentation is minimal, but if you don't care about polygon names etc, you can create a "quick and dirty" world map as follows:
# get your new map as a simple list of polygons (or lines)
z1=maps::map(maptools::readShapePoly("india_map"), plot=FALSE)
# create internal representation
z2=mapMaker::map.make(z1)
# write binary files:
mapMaker::map.export.bin(z2, "/my/path/to/world")
# To make map() call this new database:
library(maps)
worldMapEnv="MYMAP"
Sys.setenv("MYMAP"="/my/path/to/") # don't add the "world" !
now map("world") will draw your version of the world map.

Difficulty reproducing stacked bar graph in R using dygraphs

I’ve been using the dygraphs R package to produce some wonderful timeseries plots, but am having great difficulty reproducing the examples located here:
http://rstudio.github.io/dygraphs/gallery-custom-plotters.html
I’m particularly interested in creating a stacked bar chart:
My data is an xts/zoo object and plots nicely using the standard dygraph function:
However, I am unsure where the dyStackedBarGroup function comes from. It appears these functions must be created, and point to the specific plotters in .js files.
I can see for the first example, how dyBarChart is created, but there is no stackedbarchar.js/stackedbargroup.js in my local dygraph installation (however I can see the file in https://github.com/rstudio/dygraphs/tree/master/inst/plotters).
I’ve attempted to source all the functions and .js files from the github page which do not appear to be made available when loading the dygraphs package locally, but I remain unsuccessful.
Am I doing something completely wrong?
set stackedGraph argument in dyOptions to TRUE. dyOptions(stackedGraph = TRUE).
The javascript file for the barchart can be found at "examples/plotters/barchart.js" of the dygraphs package directory.
Data:
lungDeaths <- cbind(mdeaths, ldeaths)
Code:
# create dygraph plotter
library('dygraphs')
dyBarChart <- function(dygraph) {
dyPlotter(dygraph = dygraph,
name = "BarChart",
path = system.file("examples/plotters/barchart.js", package = "dygraphs"))
}
dygraph(lungDeaths) %>% # create dygraph of lungDeaths
dyBarChart() %>% # create bar chart with the passed dygraph
dyOptions(stackedGraph = TRUE) # make it as stacked bar chart

saving R leaflet map as html: tiles not included

I am trying to save an R leaflet map, using saveWidget() or outputting an rmd-file to html - as described here: Saving leaflet output as html
When generating an html-file with markdown, the map will show fine in the internal rstudio viewer, however if I open the generated html file or the html-file produced by saveWidget() in a browser, only the circles are shown, not the tiles.
Minimal example:
```{r}
library(leaflet)
library(htmlwidgets)
m <- leaflet(data.frame(lat = 55.71654, lng = 12.47484))
m <- addCircles(m, popup = "testpopup")
m <- addTiles(m)
m
saveWidget(m, file="testmap.html")
```
Add %>% addProviderTiles(providers$OpenStreetMap), that worked for me. names(providers) gives you a list of the layers
Leaflet maps are good for an interactive display but not for an export. I use ggmap package to display statics maps the can be saved as PNG file.

Trying to get multiple tooltip/hover text values using ChoroplethrZip/Plotly

I'm trying to draw a zip code level choropleth using the choroplethrZip package, and Plotly to get hover text/tooltips. choroplethrZip plots are ggplot2 objects, so I can make a zip code choropleth using the zip_choropleth function, and then use the ggplotly() function of plotly to render a choropleth where the pre-defined bins are the hover text. I'd like for the hover text to be the "region" column in my data frame, as well as the "value" column. The tooltip argument of ggplotly displays the aesthetic mappings, but choroplethrZip doesn't let me specify those. I tried adding them by using
+geom_blank(aes(text=region))
, but I get the same result, the hover text is just the bin that the zip belongs to.
Example code:
library(choroplethrZip)
library(choroplethr)
library(choroplethrMaps)
library(plotly)
library(ggplot2)
df <- get_zip_demographics(endyear= 2013, span=5)
colnames(df)[2] <- c("value")
zipplot <- zip_choropleth(df, state_zoom = "alabama")
ggplotly(zipplot)
Edited to add a library necessary for the code example to work. Also, the choroplethrZip package isn't on CRAN, so here is a link with installation instructions:
https://github.com/arilamstein/choroplethrZip

Resources