2016-02-29 105 views
1

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

在你的問題中,你已經使用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

+0

@temor看看[單張R](https://rstudio.github.io/leaflet/),特別是[legents](https://rstudio.github.io/leaflet/legends.html)頁面和[光柵圖像]頁面(https://rstudio.github.io/leaflet/raster.html)頁面 – SymbolixAU

+0

@temor看着[錯誤消息](https://github.com/rstudio/leaflet/blob/master/R/colors.R),它表明你的範圍內有一些「無限」的值。什麼是'df $ t [!is.finite(df $ t)]'? – SymbolixAU

+0

我解決它,因爲「」但我仍然有傳奇的大小問題 – temor