我正在尝试使用R中的highcharter创建这样的运动图http://jkunst.com/r/adding-motion-to-choropleths/
但我有一个问题:数据无法可视化
df<-structure(list(fips = c("ARG", "ARG", "ARG", "ARG", "ARG", "ARG",
"ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG",
"ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG",
"ARG", "ARG", "ARG", "ARG", "AUS", "AUS", "AUS", "AUS", "AUS",
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS",
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS",
"AUS", "AUS", "AUS", "AUS", "AUS", "CAN", "CAN", "CAN", "CAN",
"CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN",
"CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN",
"CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "DEU", "DEU", "DEU",
"DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU",
"DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU",
"DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "GBR", "GBR",
"GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR",
"GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR",
"GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "ITA",
"ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA",
"ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA",
"ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA",
"ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU",
"ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU",
"ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU",
"ROU", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS",
"RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS",
"RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS",
"RUS", "RUS", "USA", "USA", "USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA"), sm = c("Sep 2016", "Oct 2016", "Nov 2016",
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017",
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017",
"Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018",
"Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018",
"Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016",
"Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017",
"Apr 2017", "May 2017", "Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017",
"Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018",
"Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018",
"Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016",
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017",
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017",
"Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018",
"Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018",
"Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016",
"Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017",
"Apr 2017", "May 2017", "Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017",
"Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018",
"Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018",
"Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016",
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017",
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017",
"Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018",
"Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018",
"Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016",
"Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017",
"Apr 2017", "May 2017", "Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017",
"Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018",
"Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018",
"Oct 2018", "Nov 2018", "Dec 2018"), value = c(0, 13, 1397, 12134,
6938, 13193, 6947, 6990, 7112, 7233, 13838, 18109, 9700, 19612,
18048, 6441, 7528, 9947, 8073, 5862, 5991, 5501, 6294, 6960,
5350, 4479, 3612, 3143, 0, 12845, 28060, 35813, 37810, 35687,
39469, 37079, 34033, 29262, 31483, 29198, 22719, 19749, 17035,
18740, 19646, 16118, 18970, 18930, 23074, 22650, 25215, 23299,
21329, 19246, 20893, 17106, 0, 11964, 33654, 54440, 51427, 55324,
51136, 41176, 40035, 33106, 34155, 34207, 26175, 21376, 16997,
17213, 18211, 15071, 17266, 15598, 15431, 16155, 16082, 18322,
17276, 17654, 17282, 14001, 0, 737, 3225, 9009, 9324, 9780, 7361,
7584, 12033, 24794, 25236, 27493, 27189, 20672, 17579, 17706,
19280, 17461, 18950, 14288, 15485, 15241, 15148, 16124, 14720,
15279, 16888, 13580, 0, 4020, 21926, 27982, 32928, 39744, 46413,
34679, 35026, 32160, 33611, 33234, 29394, 23364, 20977, 23407,
26501, 21561, 23497, 20090, 21115, 18817, 18255, 21492, 21852,
19908, 18862, 16447, 0, 218, 2611, 8126, 5907, 10862, 5104, 4956,
13169, 22457, 25120, 18480, 15580, 14720, 13146, 16750, 16321,
18067, 15336, 13460, 12949, 9010, 10691, 12153, 13791, 16114,
12893, 10122, 0, 60, 1143, 4251, 3754, 6211, 3291, 2570, 5673,
8171, 9279, 12914, 9675, 7521, 5621, 9473, 7104, 12808, 10212,
8591, 6588, 5055, 6374, 6811, 8072, 11054, 9431, 9558, 0, 122,
1004, 2713, 2554, 2642, 3946, 4355, 3526, 3945, 3792, 5631, 7642,
11354, 13887, 12825, 16926, 15920, 14848, 13762, 13429, 10786,
11194, 11214, 9251, 11578, 13808, 14115, 2, 43253, 213319, 332907,
366366, 368012, 392814, 302207, 283924, 260065, 274796, 269966,
235781, 190387, 166872, 195390, 207261, 166208, 176403, 178703,
192598, 191750, 207203, 211225, 184818, 192932, 214297, 179867
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-252L))
my_ds <- df %>%
group_by(fips) %>%
do(item = list(
fips = first(.$fips),
sequence = .$value,
value = first(.$value))) %>%
.$item
url <- "https://code.highcharts.com/mapdata/custom/world.js"
tmpfile <- tempfile(fileext = ".json")
download.file(url, tmpfile)
us <- readLines(tmpfile)
us <- gsub(".* = ", "", us)
map <- jsonlite::fromJSON(us, simplifyVector = FALSE)
highchart(type = "map") %>%
hc_add_series(data = my_ds,
mapData = map,
joinBy = "fips",
borderWidth = 0.01) %>%
hc_colorAxis(stops = color_stops()) %>%
hc_title(text = "How the Epidemic of Drug Overdose Deaths Ripples") %>%
hc_legend(layout = "vertical", reversed = TRUE,
floating = TRUE, align = "right") %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_motion(
enabled = TRUE,
axisLabel = "year",
labels = unique(as.character(df$sm)),
series = 0,
updateIterval = 50,
magnet = list(
round = "floor",
step = 0.1
)
)
答案 0 :(得分:3)
问题是您使用的世界的地图数据没有fips
(这些是美国的县代码),因此joinBy="fips"
无法按您期望的方式工作。
数据集的国家/地区代码对应于世界地图iso-a3
代码。如果您使用这些而不是fips
,它将按预期工作:
my_ds <- df %>% rename(`iso-a3` = fips) %>%
group_by(`iso-a3`) %>%
do(item = list(
`iso-a3` = first(.$`iso-a3`),
sequence = .$value,
value = first(.$value))) %>%
.$item
highchart(type = "map") %>%
hc_add_series(data = my_ds,
mapData = map,
joinBy = "iso-a3",
borderWidth = 0.01) %>%
hc_colorAxis(stops = color_stops()) %>%
hc_title(text = "How the Epidemic of Drug Overdose Deaths Ripples") %>%
hc_legend(layout = "vertical", reversed = TRUE,
floating = TRUE, align = "right") %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_motion(
enabled = TRUE,
axisLabel = "year",
labels = unique(as.character(df$sm)),
series = 0,
updateIterval = 50,
magnet = list(
round = "floor",
step = 0.1
)
)