使用Leaflet自定义气泡地图的图例。

huangapple go评论83阅读模式
英文:

Customize the legend of bubble map by using leaflet

问题

到目前为止,我已经能够绘制如下的气泡地图:

library(leaflet)
library(leafletCN)
library(plyr)

dat = data.frame(name = regionNames("china"),
                 value = 1)

data=data.frame(city=c("NJ","AH","BJ","CD","CQ"),
                 count=c(15,32,45,52,24),
                 lat=c(32,31,39,29,29),
                 long=c(118,117,116,111,106))

data2 <- data %>%
  arrange(count) %>%
  mutate(name=factor(city, unique(city))) %>%
  mutate(mytext=paste(
    "City: ", city, "\n", 
    "Population: ", count, sep="")
  )
data2

mybins <- seq(0, 60, by=10)
mypalette <- colorBin(palette="inferno", domain=data2$count, na.color="transparent", bins=mybins)

geojsonMap(dat,"china") %>%
  addCircleMarkers(data=data2,~long, ~lat, 
                   fillColor = ~mypalette(count), popup = ~mytext,
                   fillOpacity = 0.7, color="white", radius=~(count/2), stroke=FALSE)%>%
  addLegend(data=data2,pal=mypalette, values=~count, opacity=0.9, title = "Population", position = "bottomright" )

使用Leaflet自定义气泡地图的图例。

那么如何删除"Legend"的图例,只保留"Population"的图例?

英文:

So far I could plot the bubble map as below:

library(leaflet)
library(leafletCN)
library(plyr)

dat = data.frame(name = regionNames(&quot;china&quot;),
                 value = 1)

data=data.frame(city=c(&quot;NJ&quot;,&quot;AH&quot;,&quot;BJ&quot;,&quot;CD&quot;,&quot;CQ&quot;),
                 count=c(15,32,45,52,24),
                 lat=c(32,31,39,29,29),
                 long=c(118,117,116,111,106))

data2 &lt;- data %&gt;%
  arrange(count) %&gt;%
  mutate(name=factor(city, unique(city))) %&gt;%
  mutate(mytext=paste(
    &quot;City: &quot;, city, &quot;\n&quot;, 
    &quot;Population: &quot;, count, sep=&quot;&quot;)
  )
data2

mybins &lt;- seq(0, 60, by=10)
mypalette &lt;- colorBin(palette=&quot;inferno&quot;, domain=data2$count, na.color=&quot;transparent&quot;, bins=mybins)

geojsonMap(dat,&quot;china&quot;) %&gt;%
  addCircleMarkers(data=data2,~long, ~lat, 
                   fillColor = ~mypalette(count), popup = ~mytext,
                   fillOpacity = 0.7, color=&quot;white&quot;, radius=~(count/2), stroke=FALSE)%&gt;%
  addLegend(data=data2,pal=mypalette, values=~count, opacity=0.9, title = &quot;Population&quot;, position = &quot;bottomright&quot; )

使用Leaflet自定义气泡地图的图例。

So how to remove the legend of Legend, and only keep the legend of Population?

答案1

得分: 1

你可以使用函数 clearControls 来移除图例。之后,你可以像这样添加你想要的图例:

library(leaflet)
library(leafletCN)
library(dplyr)

geojsonMap(dat,"china") %>%
  clearControls() %>%
  addCircleMarkers(data=data2,~long, ~lat, 
                   fillColor = ~mypalette(count), popup = ~mytext,
                   fillOpacity = 0.7, color="white", radius=~(count/2), stroke=FALSE)%>%
  addLegend(data=data2,pal=mypalette, values=~count, opacity=0.9, title = "Population", position = "bottomright" )

使用Leaflet自定义气泡地图的图例。

创建于2023-06-26,使用 reprex v2.0.2

英文:

You could use the function clearControls to remove the legend. After that you could add the legend you want like this:

library(leaflet)
library(leafletCN)
library(dplyr)

geojsonMap(dat,&quot;china&quot;) %&gt;%
  clearControls() %&gt;%
  addCircleMarkers(data=data2,~long, ~lat, 
                   fillColor = ~mypalette(count), popup = ~mytext,
                   fillOpacity = 0.7, color=&quot;white&quot;, radius=~(count/2), stroke=FALSE)%&gt;%
  addLegend(data=data2,pal=mypalette, values=~count, opacity=0.9, title = &quot;Population&quot;, position = &quot;bottomright&quot; )

使用Leaflet自定义气泡地图的图例。<!-- -->

<sup>Created on 2023-06-26 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月26日 14:44:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定