2022/05/choropleth-map-animation/ #56
Replies: 6 comments
-
在自定义坐标投影方面, @helgasoft 基于 Apache Echarts 的 demo 用 echarty 包实现代码。 |
Beta Was this translation helpful? Give feedback.
-
Great article! library(maps)
votes_repub <- as.data.frame(votes.repub)
votes_repub = within(votes_repub, {
state_name = rownames(votes_repub)
state = tolower(rownames(votes_repub)) # 州名转小写
})
us_votes_repub <- reshape(
data = votes_repub,
# 需要转行的列,列名构成的向量
varying = as.character(seq(1856, 1976, by = 4)),
times = as.character(seq(1856, 1976, by = 4)), # 构成新列 year 的列值
v.names = "repub_prop", # 列转行 列值构成的新列,指定名称
timevar = "year", # 列转行 列名构成的新列,指定名称
idvar = "state", # 州名
new.row.names = 1:(31 * 50),
direction = "long"
)
mapurl <- "https://fastly.jsdelivr.net/gh/apache/echarts-website@asf-site/examples/data/asset/geo/USA.json"
usaJson <- jsonlite::read_json(mapurl)
library(echarty)
p <- us_votes_repub |> # subset(subset= year>"1970") |>
group_by(year) |>
ec.init(js= 'projection = d3.geoAlbersUsa();',
tl.series= list(
type= 'map',
encode= list(name='state_name', value='repub_prop')
)
) |>
ec.plugjs('https://fastly.jsdelivr.net/npm/d3-array') |>
ec.plugjs('https://fastly.jsdelivr.net/npm/d3-geo')
p$x$registerMap <- list(list(mapName='USA', geoJSON= usaJson))
p$x$opts$geo <- list(map= 'USA', roam= TRUE,
projection= list(
project= htmlwidgets::JS('function(point) {return projection(point);}'),
unproject= htmlwidgets::JS('function(point) {return projection.invert(point);}')
)
)
p$x$opts$xAxis <- p$x$opts$yAxis <- NULL # delete redundant presets
p$x$opts$options <- lapply(p$x$opts$options, function(op) {
# update preset titles
op$title$right <- 100; op$title$top <- 50
op$title$textStyle <- list(
fontSize = 100,
color = "rgb(170, 170, 170, 0.5)",
fontWeight = "bolder"
)
# add 'permanent' top title to all options
op$title <- list(op$title, list(
text= "1856-1976 年美国历届大选中共和党在各州的得票率",
subtext= "数据源:maps 包内置数据集 votes.repub",
sublink= "https://cran.r-project.org/package=maps/",
left= "center"))
op
})
p$x$opts$visualMap <- list(left= 'right',
min= min(us_votes_repub$repub_prop, na.rm=T),
max= max(us_votes_repub$repub_prop, na.rm=T),
inRange= list(color= RColorBrewer::brewer.pal(n= 11, name = "RdBu")),
text = c("高", "低"), calculable= TRUE)
p$x$opts$tooltip <- list(show=TRUE)
p$x$opts$timeline <- append(p$x$opts$timeline, list(autoPlay=TRUE, bottom=20)) # update preset
p
# p |> ec.inspect() # inspect JS code generated |
Beta Was this translation helpful? Give feedback.
-
@helgasoft echarty is great, Thank you! |
Beta Was this translation helpful? Give feedback.
-
Here is output made by @helgasoft echarty.mov |
Beta Was this translation helpful? Give feedback.
-
目前,R 包 echarts4r 正式发布的版本 0.4.3 支持到 Apache Echarts 5.2.2,而 给 |
Beta Was this translation helpful? Give feedback.
-
截止 2022-05-28 北京时间下午 6点:echarts4r 包内置的 JS 库 Apache Echarts 升级到 5.3.2,R 包现已更新至 0.4.4 版本,已正式发布在 CRAN 上了,复现文章中的图 12,不需要从 Github 上安装开发版了。 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
动态地区分布图 - Xiangyun Huang
https://xiangyun.rbind.io/2022/05/choropleth-map-animation/
Beta Was this translation helpful? Give feedback.
All reactions