I try to display an arrow on the map using R and the leaflet
package,
code of map see my related question:
# install packages
install.packages(c("rgdal", "maptools", "leaflet", "htmlwidgets"), dependencies = TRUE)
# load libraries
library("rgdal")
library("maptools")
library("leaflet")
library("htmlwidgets")
# load france as shape
FRA=readShapePoly("data/france.shp")
# Initialsation
m <- leaflet(padding = 0)
# Add country
m <- addPolygons(map = m, data = FRA, opacity = 100,
color = "#FAFCFA",
weight = 0.25,popup = NULL,
options = list(clickable = FALSE),
fill = T, fillColor = "#B3C4B3",
fillOpacity = 100)
# Dimention of the map
m$width <- 874
m$height <- 700
# Export as HTML file
saveWidget(m, 'mapfrance.html', selfcontained = FALSE)
code of arrow :
x<-c(2.484722,-8.445833)
y<-c(51.5085300,10.50306)
i <- order(x, y); x <- x[i]; y <- y[i]
## draw arrows from point to point :
s <- seq(length(x)-1) # one shorter than data
plot(FRA)
points(x,y)
arrows(x[s], y[s], x[s+1], y[s+1], col = 1:3,angle = 20)
how can I display a simple arrow on the map?
-
2Did you already take a look at examples, or implemented solutions? Like meteotest.github.io/leaflet-arrowsnebi– nebi2015年11月30日 08:11:26 +00:00Commented Nov 30, 2015 at 8:11
-
it's json, how to execute from R?zina_GIS– zina_GIS2015年11月30日 08:35:22 +00:00Commented Nov 30, 2015 at 8:35
-
As of 2022, leaflet.minicharts seems to provide this feature, see addFlows.NiklasvMoers– NiklasvMoers2022年01月16日 10:54:21 +00:00Commented Jan 16, 2022 at 10:54
2 Answers 2
You can't just write the r plots into your map output. Check leaflet for R for more details.
The best solution would be to use the json arrow as said from @nebi, but I also don't know how to implement it into r.
Another solution is to write a function which returns you the coordinates of an arrowhead and then just draw some lines (see example below). The arrowhead doesn't look too nice 'cuz of the use of longitudes and latitudes for the calculation. But anyway it works.
# install packages
install.packages(c("rgdal", "maptools", "leaflet", "htmlwidgets"), dependencies = TRUE)
# load libraries
library("rgdal")
library("maptools")
library("leaflet")
library("htmlwidgets")
# function get arrowhead() returns coordinates of a the arrowhead of a line
get_arrowhead <- function (fromPoint, toPoint){
# dx,dy = arrow line vector
dx <- toPoint$x - fromPoint$x;
dy <- toPoint$y - fromPoint$y;
# normalize
length <- sqrt(dx * dx + dy * dy);
unitDx <- dx / length;
unitDy <- dy / length;
# increase this to get a larger arrow head
arrowHeadBoxSize = 1;
arrowPoint1 <- list(x = (toPoint$x - unitDx * arrowHeadBoxSize - unitDy * arrowHeadBoxSize),
y = (toPoint$y - unitDy * arrowHeadBoxSize + unitDx * arrowHeadBoxSize));
arrowPoint2 <- list(x = (toPoint$x - unitDx * arrowHeadBoxSize + unitDy * arrowHeadBoxSize),
y = (toPoint$y - unitDy * arrowHeadBoxSize - unitDx * arrowHeadBoxSize));
return( mapply(c, arrowPoint1, toPoint, arrowPoint2) )
}
# load france as
FRA=readShapePoly("france.shp")
m <- leaflet(padding = 0)
# Add country
m <- addPolygons(map = m, data = FRA, opacity = 100,
color = "#FAFCFA",
weight = 0.25,popup = NULL,
options = list(clickable = FALSE),
fill = T, fillColor = "#B3C4B3",
fillOpacity = 100)
x<-c(2.484722,-8.445833)
y<-c(51.5085300,10.50306)
i <- order(x, y); x <- x[i]; y <- y[i]
fromPoint <- list (x = x[1], y = y[1])
toPoint <- list (x = x[2], y = y[2])
# get coordinates of arrowhead
arrowhead <- get_arrowhead (fromPoint, toPoint)
# draw points
m <- addCircles(map = m, lng = x, lat = y, weight = 1, radius = 1000)
# draw polyline
m <- addPolylines(map = m, lng = x, lat = y ) # arrow line
m <- addPolylines(map = m, arrow_data[,"x"], lat = arrow_data[,"y"] ) # arrow head
# Dimention of the map
m$width <- 874
m$height <- 700
# Export as HTML file
saveWidget(m, 'mapfrance.html', selfcontained = FALSE)
-
@witich : how to add the angle to show the direction of arrows , it's possible?zina_GIS– zina_GIS2016年01月02日 19:30:33 +00:00Commented Jan 2, 2016 at 19:30
I can't add a comment, but you could create an icon and use addMarker
check this out : https://rstudio.github.io/leaflet/markers.html