2022/02/maps-in-r/ #57
Replies: 4 comments 12 replies
-
斐济地震带添加汤加火山位置echarts4r 包提供 library(echarts4r)
# 汤加火山位置
tonga <- data.frame(long = 183.7499, lat = -21.0744, mag = 7, name = "汤加火山")
# 绘图
quakes |>
e_charts(x = long) |>
e_geo(
roam = TRUE,
map = "world",
boundingCoords = list(
c(165.67, -38.59),
c(188.13, -10.72)
)
) |>
e_scatter(
serie = lat,
scale = NULL, # 去掉尺度变换
size = mag,
name = "地震点",
coord_system = "geo"
) |>
e_visual_map(
serie = mag, # 震级图例
right = 0, bottom = 0, # 图例的位置,离容器右侧和下侧的距离
text = c("高", "低"), # 定义两端的文本
textStyle = list(color = "white"),
inRange = list(
color = hcl.colors(10), # 填充颜色
colorAlpha = 0.7, # 设置透明度
symbolSize = c(1, 15) # 设置气泡大小
)
) |>
e_labels(
position = "top",
formatter = htmlwidgets::JS("function(params){return( params.name )}")
) |>
e_tooltip(
formatter = htmlwidgets::JS("
function(params) {
return('<strong>' + params.seriesName + '</strong>' +
'<br>经度:' + params.value[0] +
'<br>纬度:' + params.value[1] +
'<br>震级:' + params.value[2])
}
")
) |>
e_title(
text = "斐济地震带",
subtext = "斐济是南太平洋上的一个岛国",
left = "center",
sublink = "https://echarts4r.john-coene.com/"
) |>
e_legend(show = FALSE) |> # 隐藏图例
e_theme(name = "chalk") |>
e_data(tonga, x = long) |>
e_scatter(
serie = lat, name = "汤加火山",
scale = NULL, size = mag, coord_system = "geo"
) |
Beta Was this translation helpful? Give feedback.
-
效果图相比于原文极大的减少了数据操作和代码量,预览图如下 |
Beta Was this translation helpful? Give feedback.
-
竟然忘了补充 ggplot2 版的散点图,我当时是有多喜欢 lattice 呀!不过,ggplot2 也是太不稳定了, library(maps)
library(mapdata)
library(ggplot2)
showtext::showtext_auto()
FijiMap <- map_data("worldHires", region = "Fiji")
ggplot(FijiMap, aes(x = long, y = lat)) +
geom_map(map = FijiMap, aes(map_id = region), linewidth = .2) +
geom_point(data = quakes, aes(x = long, y = lat, colour = mag)) +
scale_colour_distiller(palette = "Spectral") +
scale_y_continuous(breaks = 5 * -9:-1) +
coord_map("ortho", orientation = c(-10, 180, 0), xlim = c(160, 195)) +
labs(colour = "震级", x = "经度", y = "纬度", title = "斐济地震带") +
theme_minimal() |
Beta Was this translation helpful? Give feedback.
-
我想用 echarts4r 绘制纽约市的地图散点图,我拿到的数据结构长下面这样,里面有纽约市各交叉路口的经纬度,记得以前绘制中国城市地图是在高德上面下载了.json 文件后导入的,但是美国纽约市的类似的.json 文件我还没找到,这块我太生疏了,湘云你知道吗?
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
空间数据可视化与 R 语言(上篇) - Xiangyun Huang
https://xiangyun.rbind.io/2022/02/maps-in-r/
Beta Was this translation helpful? Give feedback.
All reactions