地图和泡沫,年份之间的过渡

时间:2017-11-22 08:39:05

标签: r ggplot2 maps plotly ggplotly

我无法使用ggplotly创建一个增长和缩小气泡的地图。我之前从未使用过,所以可能有一个明显的解决方案。我真的没有必要尝试描述问题,而不仅仅是告诉你问题是什么。这很明显。 This link为您提供以下互动地图。

enter image description here

正如你所看到的,气泡正在迅速增长和缩小,但问题是,每隔一年,气泡就会出现在多边形的后面。这是唯一的问题,分别绘制年份可以正常工作。

这是代码,你应该能够复制和运行。请注意,我使用ggpmap包中的geocodes来下载每个国家/地区的坐标。每天有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)

0 个答案:

没有答案