R 如何在plotly::ggplotly()动画中删除不需要的线/连接?
我使用plotly库创建了一个动画,该库调用带有R 如何在plotly::ggplotly()动画中删除不需要的线/连接?,r,plotly,sf,ggplotly,R,Plotly,Sf,Ggplotly,我使用plotly库创建了一个动画,该库调用带有geom\u sf()层的ggplot。当我运行动画时,我会在贴图层中的未连接点之间得到奇怪的、随机的线或连接,这是我不想要的 我看到了a(即,应该由x升序排序),但我尝试了对点进行排序,但仍然遇到了这个问题 我创建了一个MRE;在浏览器中加载“scotland.html”文件时,它应显示类似的行为: library("dplyr") library("tidyr") library("sf") library("ggplot2") library
geom\u sf()
层的ggplot。当我运行动画时,我会在贴图层中的未连接点之间得到奇怪的、随机的线或连接,这是我不想要的
我看到了a(即,应该由x升序排序),但我尝试了对点进行排序,但仍然遇到了这个问题
我创建了一个MRE;在浏览器中加载“scotland.html”文件时,它应显示类似的行为:
library("dplyr")
library("tidyr")
library("sf")
library("ggplot2")
library("plotly")
library("htmlwidgets")
tmp <- tempdir()
tmpfile <- paste0(tmp, "/scotland.zip")
url <- "https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/Scotland_ol_1991.zip"
download.file(url, tmpfile)
unzip(tmpfile, exdir = tmp)
scotland <- read_sf(tmp, "scotland_ol_1991")
points <- data.frame(
id = 20:1,
year = 20:1,
east = sample(7459:469817, size = 20),
north = sample(530297:1219574, size = 20)
)
points <-
points %>%
uncount(year) %>%
group_by(id) %>%
mutate(year = row_number()) %>%
ungroup()
# Try rearranging by x coordinate as suggested by this post:
# https://community.plot.ly/t/scatterplot-lines-unwanted-connecting/8729/3
scotland_order =
scotland %>%
group_by(name) %>%
st_coordinates() %>%
as_tibble() %>%
group_by(L2) %>%
summarise(min_x = min(X))
scotland =
scotland %>%
mutate(order = scotland_order$min_x) %>%
arrange(order) %>%
select(-order)
points <- st_as_sf(points, coords = c("east", "north"))
points <- st_set_crs(points, 27700)
p <- ggplotly(
ggplot() +
geom_sf(data = scotland) +
geom_sf(data = points, aes(frame = year, ids = id))
)
saveWidget(p, "scotland.html")
库(“dplyr”)
图书馆(“tidyr”)
图书馆(“sf”)
图书馆(“ggplot2”)
图书馆(“阴谋地”)
库(“htmlwidgets”)
tmp%
st_坐标()%>%
as_tible()%>%
分组依据(L2)%>%
总结(最小值x=最小值x))
苏格兰=
苏格兰%>%
变异(订单=苏格兰\订单$min\u x)%>%
安排(订单)%>%
选择(-order)
嗯,不确定调整顺序是否有帮助。可能是Ggly的一个bug。你是否考虑过用原生的脚本来代替?@乌瑟尔,我不认为订购数据也能解决问题,但我认为这是值得一试的。我希望不必学习原生的plotly语法,因为它已经在R
中出现了,但也许我需要学习,不确定调整顺序是否有帮助。可能是Ggly的一个bug。你是否考虑过用原生的脚本来代替?@乌瑟尔,我不认为订购数据也能解决问题,但我认为这是值得一试的。我希望不必学习原生的plotly语法,因为它已经在R
中出现了,但也许我需要学习