lcr*_*rin 5 r ggplot2 r-plotly
我正在尝试使用plotly 制作交互式ggplot2 地图。
在下面的代码中,我创建了一张法国地图,每个部门都有不同的颜色:
library(tidyverse)
library(stringr)
library(stringi)
library(maps)
library(ggplot2)
map <- map_data("france")
map_theme <- theme(title=element_text(),
plot.title=element_text(margin=margin(20,20,20,20), size=18, hjust = 0.5),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.grid.major= element_blank(),
panel.background= element_blank())
p = ggplot(map, aes(long,lat, group = group, fill = region)) +
geom_polygon() +
coord_map() +
theme(legend.position = "none") +
map_theme
plot(p)
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
plot(p) 和 ggplotly(p) 会给出类似的图表,但用plotly提供的图表看起来被拉伸了。地图有问题。避免这种情况的最佳策略是什么?
除了使用之外,coord_map()您还可以尝试使用coord_fixed(). 它并不完美,但它让你更接近。我对这个答案的灵感来自于这个帖子:How to fix the宽高比 in ggplot?
p2 = ggplot(map, aes(long,lat, group = group, fill = region)) +
geom_polygon() +
coord_fixed(1.50) +
theme(legend.position = "none") +
map_theme
plot(p2)
ggplotly(p2)
Run Code Online (Sandbox Code Playgroud)