简体   繁体   English

在R中使用小叶发布绘图(简单)多边形

[英]Issue plotting (simple) polygons with leaflet in r

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. 我想用小叶来帮助我在英国绘制热图多边形,但是即使在非常简单的情况下,leaflet()%>%addPolygons(...)也给了我奇怪的输出。

I'm using the following code to plot a simple triangle in leaflet and then ggplot2. 我正在使用以下代码在传单中绘制一个简单的三角形,然后在ggplot2中绘制。 ggplot2 has no issues, but leaflet will sometimes just give me a straight line instead of a triangle. ggplot2没有问题,但是传单有时只会给我一条直线而不是三角形。 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) geom_polygon代码: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): 但是,即使使用简单的修改也会在传单中产生一条简单的水平线(但在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 谢谢汤姆

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: 从形状文件(带有Eastings&Northings)开始,以下内容将读取到shapefile中,转换为longlat,然后使用传单成功绘制多边形:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM