英文:
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" )
那么如何删除"Legend"的图例,只保留"Population"的图例?
英文:
So far I could plot the bubble map as below:
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" )
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" )
创建于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,"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" )
<!-- -->
<sup>Created on 2023-06-26 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论