How to display labels for overlapping polygons with leaflet - leaflet

I'm trying to display labels of several overlapping polygons but unfortunately only the upper polygons gets a label.
Here is a reproductible example :
poly_a <-st_sf(st_sfc(st_polygon(list(as.matrix(data.frame(lng = c(0, 0.5, 2, 3,0),
lat = c(0, 4, 4, 0,0))))), crs = 4326))%>%
rename(geometry=st_sfc.st_polygon.list.as.matrix.data.frame.lng...c.0..0.5..2..)%>%
mutate(ID= "poly_a")
poly_b <- st_sf(st_sfc(st_polygon(list(as.matrix(data.frame(lng = c(1, 1.5, 1.8,1),
lat = c(2, 3, 2,2))))), crs = 4326))%>%
rename(geometry=st_sfc.st_polygon.list.as.matrix.data.frame.lng...c.1..1.5..1.8..)%>%
mutate(ID= "poly_b")
layer <- rbind(poly_a,poly_b)
pal <- colorFactor(
palette = c("red","green"),
domain = layer$ID)
label <- sprintf(
"<strong>%s",
layer$ID
) %>% lapply(htmltools::HTML)
leaflet(layer) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(
fillColor = ~pal(ID),
color="black",
label=label,
opacity = 0.5,
fillOpacity = 0.5)%>%
addLegend("topleft", pal = pal, values = layer$ID,
title = "Legend",
opacity = 0.25)
I would like to see "poly_a, poly_b" when I pass my mouse on the smallest polygon and "poly_a" when it s only on the biggest one.
I saw maybe I could use the plugin "point in polygon" but I don't know how.
Thank you for your help !
Paco

Related

Cannot get the color to work in a biplot from the RDA procedure in Vegan

I have run RDA from Vegan, and cannot generate four different colors in the "sites" on the plot that correspond to the Group trait which really is countries. The problem is colvec[Group] that fails to link up the four colors with the four different countries. I avoided this for the legend by just spelling out the colors for each country, and that worked (produced a legend with the correct colors for the country). When I take out the col = colvec[Group], the plot does produce the points ("sites"), but they all are black in color and do not differentiate the countries. I have been using the suggested code at:
https://fromthebottomoftheheap.net/2012/04/11/customising-vegans-ordination-plots/
Can anyone help suggest a modification that would produce the correct colors in the points (sites)?
Thanks much.
plot(R EDUNENVONLY)
scl <- 2
par(pty="s") #makes plot square
colvec <- c("dark green", "red", "purple", "blue")
Group <- ENVIR$Group
plot(REDUNENVONLY, choices = c(1, 2), type = "n", xlim = c(-1.5, 1.5))
with(ENVIR, points(REDUNENVONLY, display = "sites",
bg = colvec[Group], col = colvec[Group],
scaling = scl, pch = 21))
text(REDUNENVONLY, display="bp", scaling = scl, cex = 1,col=1) # add black arrows
with(ENVIR, legend("topright", legend = c("CCHINA", "JAPAN", "KOREA", "NCHINA"),
col = c("dark green", "red", "purple", "blue"), pch = c(16,16)))
[![Biplot produced from code above](https://i.stack.imgur.com/zTqHW.png)](https://i.stack.imgur.com/zTqHW.png)
[![Biplot without col = colvec[Group] ](https://i.stack.imgur.com/G4XjU.png)](https://i.stack.imgur.com/G4XjU.png)
I have tried various other solutions, and I know ggplot is another possibility, but this would require more learning (I am a novice at R) and surely some simply change to the present code will get this to work.

Set manually the map scale on tmap

I am creating a map with tmap to plot specific coordinates as dots.
I would like to save my map as .png and this works well using tmap_leaflet and mapshot (see code below).
library(sf)
library(tmap)
library(mapview)
coord <- data.frame(Lat=c(0.92, 0.92, 0.93, 0.92, 0.93, 0.93, 1.00, 1.00, 0.99, 0.93),
Lon=c(104.58, 104.51, 104.57, 104.50, 104.55, 104.51, 104.59, 104.49, 104.6, 104.61))
sdat <- st_as_sf(coord, coords = c("Lon", "Lat"),
crs = "+proj=longlat +datum=WGS84 +no_defs")
tmap_mode("view")
Map <- tm_basemap("Esri.WorldTopoMap") +
tm_shape(sdat) +
tm_dots(alpha = 1,
title = "Location")
lf <- tmap_leaflet(Map)
mapshot(lf, file = "Map.png") # save map
My issue is the default area that gets visualized in tmap view mode.
I would like to zoom out to visualise a larger area of the map (setting specific boundaries for instance) and not only the area really close to the points plotted. I couldn't find a solution online yet.
This is how I get the .png:
And this is what I would like to get for instance (made with a screenshot):
You can adjust the zoom levels using tm_view():
library(sf)
library(tmap)
library(mapview)
coord <- data.frame(Lat=c(0.92, 0.92, 0.93, 0.92, 0.93, 0.93, 1.00, 1.00, 0.99, 0.93),
Lon=c(104.58, 104.51, 104.57, 104.50, 104.55, 104.51, 104.59, 104.49, 104.6, 104.61))
sdat <- st_as_sf(coord, coords = c("Lon", "Lat"),
crs = "+proj=longlat +datum=WGS84 +no_defs")
tmap_mode("view")
Map <- tm_basemap("Esri.WorldTopoMap") +
tm_shape(sdat) +
tm_dots(alpha = 1,
title = "Location") +
tm_view(set.zoom.limits = c(10, 20))
lf <- tmap_leaflet(Map)
mapshot(lf, file = "Map.png")

Finding coordinates from heading and distance in R

I want to get the coordinates of new points, preferably using the sf package, when the inital positions and the distance and heading traveled is known.
Consider this; we have three points (pts), with a heading and a distance in km attached. How to find the coordinates for the new positions?
library(data.table, dplyr, sf)
dat <- data.table(lon = c(10,10.1,10.4), lat = c(58.4,57.4,57.8),
heading = c(45,10,235), distance_km = c(1,5.3,3))
pts <- dat %>%
sf::st_as_sf(coords = c("lon","lat")) %>%
sf::st_set_crs(4326)
Simple feature collection with 3 features and 2 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 10 ymin: 57.4 xmax: 10.4 ymax: 58.4
Geodetic CRS: WGS 84
heading distance_km geometry
1 45 1.0 POINT (10 58.4)
2 10 5.3 POINT (10.1 57.4)
3 235 3.0 POINT (10.4 57.8)
Was considering making circles around the points, but dont know how to connect the point to the circle with the correct heading.
buf <- st_buffer(pts, dist = pts$distance_km*1000)
circ <- st_cast(buf, "LINESTRING")
Found the answer here: Calculate coordinate from starting point, having distance and an angle for all quadrants and here:
Convert radians to degree / degree to radians
Will post my R solution for completeness. If anyone has a better or more smooth solution, feel free to post it.
library(data.table, sf, mapview)
dat <- data.table(lon = c(10,10.1,10.4), lat = c(58.4,57.4,57.8),
heading = c(45,10,235), distance_km = c(1,5.3,3))
pts <- dat %>%
sf::st_as_sf(coords = c("lon","lat")) %>%
sf::st_set_crs(4326)
pts <- st_transform(pts, 32632)
pts$utm_n <- st_coordinates(pts)[,1]
pts$utm_e <- st_coordinates(pts)[,2]
buf <- st_buffer(pts, dist = pts$distance_km*1000)
circ <- st_cast(buf, "LINESTRING")
rad2deg <- function(rad) {(rad * 180) / (pi)}
deg2rad <- function(deg) {(deg * pi) / (180)}
pts$newp_e <- pts$utm_e + (pts$distance_km*1000* cos(deg2rad(pts$heading)))
pts$newp_n <- pts$utm_n + (pts$distance_km*1000* sin(deg2rad(pts$heading)))
dt <- data.table(pts)
pts2 <- dt %>%
sf::st_as_sf(coords = c("newp_n", "newp_e")) %>%
sf::st_set_crs(32632)
mapview(pts2) + mapview(pts, zcol = "heading") + mapview(circ)

rbokeh color argument- legend messes up on R 3.6

The legend generated for an ly_bar figure that uses a variable for a color argument has a title of base::quote("variable_name"). This is only a problem when generating graphs on R 3.6.
figure() %>%
ly_bar(variety, yield, color = year, data = lattice::barley, hover = TRUE) %>%
theme_axis("x", major_label_orientation = 90)
The graph legend should say "year" instead of base::quote("year").

Render multiple BarChartDataSets using danielgindi/Charts

I'm trying to create a bar chart using danielgindi/Charts (based on MPAndroidChart). I would like to use multiple data sets, but when I try to provide multiple sets to the BarChartView no bars are rendered.
let barView = BarChartView()
let set1 = BarChartDataSet()
set1.addEntry(BarChartDataEntry(x: 1, y: 1))
let set2 = BarChartDataSet()
set2.addEntry(BarChartDataEntry(x: 2, y: 2))
barView.data = BarChartData(dataSets: [set1, set2])
contentView = barView
In the above example, I would expect two bars to be displayed. However, no bars will appear at all.
If I provide only one data set, the chart will render as expected.
barView.data = BarChartData(dataSets: [set1])
The problem is that with multiple groups, the position of the bars on the x-axis will change. However, the axis minimum and maximum values do not automatically resize to fit the new data. They must be resized manually. For example:
let groupSpace = 0.3
let barSpace = 0.0
let groupWidth = barData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
columnView.xAxis.axisMinimum = 0
columnView.xAxis.axisMaximum = groupWidth * numOfEntriesPerSet
columnView.groupBars(fromX: 0, groupSpace: groupSpace, barSpace: barSpace)