如何在地图上显示点的图例(比例颜色)?

时间:2016-02-29 21:06:19

标签: r

版本R 3.2.3

 library(OpenStreetMap)
 map <- openmap(c(lat= 48,   lon= 3),   c(lat= 40,   lon= 0))
 map <- openproj(map) 
 plot(map)
 lon=c(1, 03, 04, 08, -1)
  lat=c(40, 41, 41, 42, 41)
  x=c(1, 3, 0.1, 2, 1) #I give here example of only 5 values but my real values are many and vary. So the values here are not discrete but continuous!

 points(lon,lat,pch=19,col=x)  #not sure here how

我想在地图上绘制x的这些点,颜色范围从蓝色到红色,最重要的是在地图旁边显示比例颜色(图例)。

1 个答案:

答案 0 :(得分:1)

在您的问题中,您使用了OpenStreetMap(我无法运行);你不喜欢使用leaflet吗?

如果没有,你可以做到

library(leaflet)

df <- data.frame(lon=c(1, 03, 04, 08, -1),
                 lat=c(40, 41, 41, 42, 41),
                 x=c(1, 3, 0.1, 2, 1))

pal <- colorNumeric(
    palette = c("#ff0000","#0000ff"),
    domain = df$x
)

leaflet() %>%
    setView(lng = 3, lat = 48, zoom = 4) %>%
    addProviderTiles("Esri.WorldGrayCanvas") %>% ## pick any map layer you want
    addCircleMarkers(data = df, lng = ~lon, lat = ~lat, stroke=FALSE, color=~pal(x), fillOpacity = 0.6) %>%
    addLegend(position = c("bottomleft"), pal = pal, values = df$x)

enter image description here