Reputation: 85
I'd like to use leaflet to help me plot heatmap polygons over the UK, but leaflet()%>%addPolygons(...) is giving me weird output, even for a very simple case.
I'm using the following code to plot a simple triangle in leaflet and then ggplot2. ggplot2 has no issues, but leaflet will sometimes just give me a straight line instead of a triangle. The leaflet code:
l <- leaflet()
shapes <- unique(df$shape)
for (shape in shapes) {
d <- df[df$shape == shape, , drop = FALSE]
l <- l %>% addPolygons(lat = d$lat, lng = d$lng,noClip=TRUE)
}
l
The geom_polygon code: qplot(lng, lat, data=df, geom="polygon", group = shape)
If I use this input, then I get sensible output from both pacakges:
df <- data.frame(lat = c(1, 2, 3), lng = c(2, 3, 1), shape = c("triangle", "triangle", "triangle"))
However, using even a simple modification leads to a simple horizontal line in leaflet (but a correct triangle in ggplot2):
df <- data.frame(lat = c(100, 200, 300), lng = c(200, 300, 100), shape = c("triangle", "triangle", "triangle"))
It sounds to me like I'm missing a parameter or something, but I can't for the life of my figure out what's going on. Any help appreciated.
Thanks, Tom
Upvotes: 1
Views: 1036
Reputation: 85
Apparently leaflet only deals with long/lat, even if there is no base maps. Starting with a shape file (with Eastings & Northings), the following will read in a shapefile, convert to longlat and then successfully plot the polygons using leaflet:
shapefile <- readOGR("filepath_here", "shapefile_here")
shapeData <- spTransform(shapefile, CRS("+proj=longlat +datum=WGS84 +no_defs"))
leaflet() %>%
addPolygons(data=subset(shapeData, POSTAREA %in% c('SW')),weight=2)
Upvotes: 3