代码之家  ›  专栏  ›  技术社区  ›  Jaccar

使用ggplotly的地图显示不正确(R)

  •  2
  • Jaccar  · 技术社区  · 8 年前

    enter image description here

    但当使用ggplotly显示时,它如下所示:

    enter image description here

    这是我使用的代码的核心:

    random_map <- ggplot()  +
      geom_map(data = random_data, aes(map_id = random_data$ï..CCG, fill = random_data$Number),  
          map = CCGshape) +
      geom_polygon (data = CCGshape, aes(x = long, y = lat, group = group),
          colour = "azure4", size = 0.01, fill = NA) +
      expand_limits(x = CCGshape$long, y = CCGshape$lat) +
      coord_equal ()
    
    random_plotly <- ggplotly(random_map)
    

    我使用的shapefile是 here .

    这是我使用的数据的头部:

    structure(list(Number = c(1, 0.4, 0.9, 0.3, 0.3, 0.7), 
    CCG = c("NHS Airedale, Wharfedale and Craven CCG", 
    "NHS Barnsley CCG", "NHS Bassetlaw CCG", "NHS Bradford Districts CCG", 
    "NHS Calderdale CCG", "NHS Bradford City CCG")), .Names = c("Number", 
    "CCG"), row.names = c(NA, 6L), class = "data.frame")
    

    知道我做错了什么吗?

    2 回复  |  直到 8 年前
        1
  •  2
  •   C-x C-c    8 年前

    这似乎是几何地图不支持(据我所知)的阴谋。相反,看一看 here . 有个包裹 sf geom 打电话 geom_sf 那也许能解决你的问题。plotly网站提供了 几何 here .

    值得一提的是,ggplotly在某种程度上解决了这样一个问题,即在我看来,ggplot的语法比plotly的语法要清晰得多。也就是说,如果你想让plots在一个被动的上下文中很好地工作,你最好按照plotly希望你的方式来做,也就是说,包含在其中一个上下文中的东西 these 教程。

        2
  •  2
  •   meenaparam    8 年前

    plotly website ,我做了下面这个简单的例子。我在你发布的链接中使用了shapefile,所以我认为它可以用来绘制你想要的地图。我随意地用 ccg18cd 字段,但您可以根据需要更改填充。

    library(plotly)
    library(sf)
    
    ccg <- sf::st_read("Clinical_Commissioning_Groups_April_2018_Ultra_Generalised_Clipped_Boundaries_in_England.shp")
    
    ccg_plot <- ggplotly(ggplot(ccg) + geom_sf(aes(fill = ccg18cd)))
    

    这将生成以下打印,带有带标签的工具提示并填充多边形。

    ccg_plotly_plot