我无法使用 ggplotly 创建具有不断增长和缩小气泡的地图。我以前从未使用过 plotly,所以可能有一个明显的解决方案。我真的没有必要尝试描述问题,而不仅仅是向您展示问题所在。这是相当明显的。此链接为您提供以下交互式地图。
如您所见,气泡正在很好地增长和缩小,但问题是每隔一年,气泡就会出现在多边形后面。这是唯一的问题,单独绘制年份可以正常工作。
这是代码,您应该能够复制和运行它。请注意,我正在使用geocodes
ggpmap 包下载每个国家/地区的坐标。每天有 2500 次免费下载的限制。
library(ggplot2)
library(plotly)
library(wbstats)
library(countrycode)
library(ggmap)
startdate = 2010
enddate = 2016
indicator <- "SM.POP.REFG"
# download data
dat <- wb(indicator = indicator,
startdate = startdate,
enddate = enddate)
# download map data
worlds <- map_data("world")
worlds$iso2c = countrycode(worlds$region,
origin = "country.name",
destination = "iso2c")
# merge map data and wb data
merged <- merge(worlds, subset(dat, date==2016), sort=F, by="iso2c")
merged <- merged[order(merged$order), ]
# download geocodes (might take a minute or two)
cords <- geocode((unique(merged$country)))
cords <- data.frame(unique(merged$iso2c),unique(merged$country),cords)
colnames(cords) <- c("iso2c","country","lon","lat")
#
valuestocords <- unique(dat[,c("value","country","date")])
newdata = merge(cords, valuestocords, by = "country")
# plot
g <- ggplot(merged, aes(long, lat))+
geom_polygon(aes(group = group), fill="grey")+
geom_point(data=newdata, aes(lon, lat, size=value, ids=country, frame=date), alpha=0.5)+
scale_size_area()
ggplotly(g)