【问题标题】:R - plotly - remove map stretchingR - plotly - 删除地图拉伸
【发布时间】:2020-04-15 13:24:02
【问题描述】:

我正在尝试使用 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)

plot(p) 和 ggplotly(p) 将给出相似的图形,但 plotly 提供的图形似乎被拉伸了。地图有问题。避免这种情况的最佳策略是什么?

【问题讨论】:

    标签: r ggplot2 r-plotly


    【解决方案1】:

    您可以尝试使用coord_fixed(),而不是使用coord_map()。它并不完美,但它让你更接近。我对这个答案的灵感来自这个帖子:How to fix the aspect ratio 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-17
      • 2023-03-29
      • 2016-05-07
      • 2020-10-14
      • 2021-11-29
      • 2014-03-29
      • 1970-01-01
      相关资源
      最近更新 更多