我在r中使用传单包制作了一个交互式地图。我创建了一个示例数据集,如下所示。有两个城市,伦敦和伯明翰。来自伦敦的3条推文和来自伯明翰的2条推文。我想要做的是单击地图上的标记以显示该位置的所有推文。但是,我只能显示在弹出窗口中单击的那个位置的第一条推文。
我要显示的是单击位置上的所有推文。理想情况是像表格或一条推文列表。这只是我组成的数据集。我的真实数据集有许多城市,其中一些城市有10多个推文。有谁知道如何做到这一点?非常感谢您的帮助。
我的代码如下:
library(leaflet)
library(dplyr)
data_map = data.frame(lat=c(51.50735, 51.50735, 51.50735,52.48624,52.48624),
lon=c(-0.1277583,-0.1277583,-0.1277583,-1.8904010,-1.8904010),
n = c(3,3,3,2,2),
location = c('London', 'London', 'London', 'Birmingham', 'Birmingham'),
tweet = c('Great', 'I love this', 'like it', 'hate it', 'worst ever'))
m = leaflet(data_map) %>%
setView(lng = -1.470085 , lat=53.38113,zoom=5) %>%
addTiles() %>%
#addProviderTiles(providers$Stamen.Toner) %>%
addCircleMarkers(
lng = ~lon,
lat = ~lat,
#popup = ~Location,
#size=3,
radius = ~n*3,
stroke = T,
fillOpacity = 0.5,
popup=paste(
"Address:", data_map$location, "<br>",
"number of tweets:", df_map$n, "<br>",
"Posts:", data_map$tweet)
)
print(m)
答案 0 :(得分:1)
您可以将数据减少到每个位置一行,并将每个位置的所有推文保存在列表列中。
library(leaflet)
library(dplyr)
data_map = data.frame(lat=c(51.50735, 52.48624),
lon=c(-0.1277583,-1.8904010),
n = c(3,2),
location = c('London', 'Birmingham'))
data_map$tweets <- list(c('Great', 'I love this', 'like it'), c('hate it', 'worst ever'))
m = leaflet(data_map) %>%
setView(lng = -1.470085 , lat=53.38113,zoom=5) %>%
addTiles() %>%
#addProviderTiles(providers$Stamen.Toner) %>%
addCircleMarkers(
lng = ~lon,
lat = ~lat,
#popup = ~Location,
#size=3,
radius = ~n*3,
stroke = T,
fillOpacity = 0.5,
popup= ~paste(
"<strong> Address: </strong>", location, "<br>",
"<strong> number of tweets: </strong>", n, "<br>",
"<strong> Posts: </strong>", "<br>", lapply(tweets, paste, collapse = "<br>")
))
m