R使用ggplotly和geom_sf更改动画地图中的标签
我想创建一个动画地图,显示一段时间内宾夕法尼亚州匹兹堡发生的事件数量(按邻里)。我可以创建地图,但我不知道如何在工具提示中添加邻居的名称。这是到目前为止我的代码R使用ggplotly和geom_sf更改动画地图中的标签,r,sf,ggplotly,R,Sf,Ggplotly,我想创建一个动画地图,显示一段时间内宾夕法尼亚州匹兹堡发生的事件数量(按邻里)。我可以创建地图,但我不知道如何在工具提示中添加邻居的名称。这是到目前为止我的代码 library(plotly) library(ggplot2) library(sf) #Downloading shapefile download.file("http://pghgis-pittsburghpa.opendata.arcgis.com/datasets/dbd133a206cc4a3aa915cb28baa6
library(plotly)
library(ggplot2)
library(sf)
#Downloading shapefile
download.file("http://pghgis-pittsburghpa.opendata.arcgis.com/datasets/dbd133a206cc4a3aa915cb28baa60fd4_0.zip",
destfile = "Pittsburgh_Neighborhoods",mode = 'wb')
unzip("Pittsburgh_Neighborhoods", exdir = ".")
Neighborhoods <- st_read("Neighborhoods_.shp") %>%
st_transform(crs = "+proj=longlat +datum=WGS84")
#Creating fake data
dates <- seq(as.Date('2017-01-01'),as.Date('2019-12-31'),by = "1 month")
count <- sample(1:30,3240,replace=T)
neigh <- Neighborhoods$hood
df <- merge(dates,neigh)
colnames(df) <- c("Month_Yr", "Neighborhood")
df$count <- count
df$Month_Yr <- format(df$Month_Yr, "%Y-%m") ##Changing to charch
#Merging data frame with neighborhoods layers
map_data <- merge(Neighborhoods, df, by.x = "hood", by.y = "Neighborhood")
#######Create plot with text
p_map2 <- ggplot() +
geom_sf(data = Neighborhoods, colour = "#ffffff20", fill = "#2d2d2d60", size = .5) +
geom_sf(data = map_data, aes(fill = count, frame = Month_Yr,
text = paste0(
"Neighborhood: ", hood, "\n",
"Month Year: ", Month_Yr, "\n",
"Number of fake incidents: ", count
))) +
ggtitle("Fake incidents over time in Pittsburgh") +
scale_fill_viridis_c(option = "magma",begin = 0.1, direction = -1) +
theme_void() +
theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(), axis.text.y=element_blank(),
axis.ticks.y=element_blank())
#animating the map using ggplotly
pg_map2 <- p_map2 %>%
ggplotly(tooltip = "text") %>% ##adding the text in the tooltip
style(hoverlabel = list(bgcolor = "white"), hoveron = "fill") %>%
plotly_build()
#for some weird reason, the map animates the polygons and not the data, correcting that...
pg_map2$x$frames <- lapply(
pg_map2$x$frames, function(f) {
f$data <- lapply(f$data, function(d) d[!names(d) %in% c("x", "y")])
f
})
#Adding animation bar
pg_map2 %>%
animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
) %>%
animation_slider(
currentvalue = list(prefix = "Month Year ", font = list(color="red"))
)
library(plotly)
图书馆(GG2)
图书馆(sf)
#下载形状文件
下载文件(“http://pghgis-pittsburghpa.opendata.arcgis.com/datasets/dbd133a206cc4a3aa915cb28baa60fd4_0.zip",
destfile=“匹兹堡社区”,模式='wb')
解压缩(“匹兹堡社区”,exdir=“.”)
社区%
st_变换(crs=“+proj=longlat+datum=WGS84”)
#创建假数据
日期我使用R3.6.1、4.9.1和ggplot“3.2.1”
我猜想,gplotly()
函数没有正确地将ggplot对象转换为plotly。转换为plotly时,sf绘图和动画可能存在一些问题。如果查看pg_map2$x$frames
对象,您将看到每个帧在数据字段中具有不同数量的元素。我认为每个元素都应该是一个邻域多边形。因此,元素的数量不应该从一个帧更改到另一个帧
我想这就是你在动画中表现出奇怪行为的原因。也许和标签改变的原因是一样的
我所做的是直接在plotly API中工作。这似乎解决了问题。我过滤了5个月的数据,只是为了使渲染更轻松(渲染整个数据集需要一些时间)
#将数据框与邻域图层合并,并添加一个包含提示文本的列
地图数据%
突变(text=paste0(“邻里:”,胡德,“\n”,“月年:”,月年,“\n”,“假事件数:”,计数))
地图数据%>%过滤器(月年百分比,单位为%c(“2017-01”、“2017-02”、“2017-03”、“2017-04”、“2017-05”))%>%
绘图(笔划=I(“黑色”)、分割=~hood、颜色=~count、文本=~text、showlegend=FALSE、hoverinfo=“text”、hoveron=“fills”、帧=~Month\u Yr)%>%
样式(hoverlabel=list(bgcolor=“white”)%%>%
动画\u滑块(当前值=列表(前缀=“月-年”,字体=列表(颜色=“红色”))%>%
布局(title=“匹兹堡一段时间内的假事件”)
在R和plotly中制作地图的两个好教程
希望这有帮助 我正在运行你的代码。在我这边,我看到“中央商务区”一直在同一个地方。我想知道是什么导致了你的情况。为什么它运行得这么慢?@jazzurro感谢你的回答。你们有什么R版本?Simon Woodward,我不确定,当我运行代码时,有时需要太长时间,有时速度很快。@NataliaP我正在windows机器上使用最新版本(3.6.2)。非常感谢!!我不知道我可以直接使用plot_y绘制地图,尽管我在阅读您推荐的教程之前就已经阅读了。我想我只关注geom_sf,跳过了其他部分。使用Plotly的唯一问题是,正如您所提到的,渲染整个数据集确实需要很多时间;使用gplotly()函数似乎运行得更快。
# Merging data frame with neighborhoods layers and adding a column with the tip text
map_data <- merge(Neighborhoods, df, by.x = "hood", by.y = "Neighborhood") %>%
mutate(text=paste0("Neighborhood: ", hood, "\n","Month Year: ", Month_Yr, "\n","Number of fake incidents: ", count))
map_data %>% filter(Month_Yr %in% c("2017-01", "2017-02", "2017-03", "2017-04", "2017-05")) %>%
plot_ly(stroke = I("black"), split=~hood, color=~count, text=~text, showlegend = FALSE, hoverinfo = "text", hoveron = "fills", frame=~Month_Yr) %>%
style(hoverlabel = list(bgcolor = "white")) %>%
animation_slider(currentvalue = list(prefix = "Month Year ", font = list(color="red"))) %>%
layout(title="Fake incidents over time in Pittsburgh")