使用ggplot在投影地图上添加R饼图时,饼图会变形。

6

我希望使用ggplot在投影地图上绘制饼图。然而,由于投影的原因,饼图会变形。有没有人知道如何在不变形的情况下绘制饼图?以下是示例代码,谢谢。

lib = c("ggplot2","scatterpie")
lapply(lib, library, character.only=TRUE)
pie = data.frame(
    lon=c(-5.0,-3.5,-5.5,5.0), 
    lat=c(50.0,50.2,50.1,50.5), 
    A=c(0.25,0.75,0,0.25), 
    B=c(0.75,0.10,0,0.75), 
    C=c(0,0.15,1,0), 
    radius=0.05)

world = map_data("world", resolution=0)

ggplot(data=world, aes(x=long, y=lat, group=group)) + 
    geom_polygon(data=world, aes(x=long, y=lat, group=group), fill="darkseagreen", color="black") + 
    coord_map(projection = "mercator",xlim=c(-7.0,-2.0), ylim=c(49,52)) + 
    geom_scatterpie(aes(x=lon, y=lat, r=0.15), data=pie, cols=c("A","B","C"), color="black", alpha=0.9) + 
    ylab("Latitude\n") + xlab("Longitude") + 
    theme(
        panel.background = element_rect(fill="lightsteelblue2"),
        panel.grid.minor = element_line(colour="grey90", size=0.5), 
        panel.grid.major = element_line(colour="grey90", size=0.5), 
        legend.position = "top")

enter image description here


如有可能,请添加一张图片 - 5th
1
我猜问题出在 coord_mapprojection 参数上(你需要找到正确的参数)。 - pogibas
1个回答

4
您可以使用annotation_custom解决坐标比例差异的问题。请注意,它仅适用于笛卡尔坐标(不包括coord_map()),但只要您能够使用coord_quickmap(),以下解决方案将有效: 步骤1:创建基础图,使用coord_quickmap()代替coord_map()。隐藏次要网格线以模仿后者的外观。否则,与您上面使用的相同。
p <- ggplot(data = world, aes(x=long, y=lat, group=group)) + 
  geom_polygon(fill = "darkseagreen", color = "black") + 
  coord_quickmap(xlim = c(-7, -2), ylim = c(49, 52)) +
  ylab("Latitude") + 
  xlab("Longitude") + 
  theme(
    panel.background = element_rect(fill = "lightsteelblue2"),
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_line(colour = "grey90", size = 0.5), 
    legend.position = "top")

步骤2:创建饼图注释:

pie.list <- pie %>% 
  tidyr::gather(type, value, -lon, -lat, -radius) %>%
  tidyr::nest(type, value) %>%

  # make a pie chart from each row, & convert to grob
  mutate(pie.grob = purrr::map(data,
                               function(d) ggplotGrob(ggplot(d, 
                                                             aes(x = 1, y = value, fill = type)) +
                                                        geom_col(color = "black",
                                                                 show.legend = FALSE) +
                                                        coord_polar(theta = "y") +
                                                        theme_void()))) %>%

  # convert each grob to an annotation_custom layer. I've also adjusted the radius
  # value to a reasonable size (based on my screen resolutions).
  rowwise() %>%
  mutate(radius = radius * 4) %>%
  mutate(subgrob = list(annotation_custom(grob = pie.grob,
                                          xmin = lon - radius, xmax = lon + radius,
                                          ymin = lat - radius, ymax = lat + radius)))

第三步。将饼图添加到底层图中:

p + 

  # Optional. this hides some tiles of the corresponding color scale BEHIND the
  # pie charts, in order to create a legend for them
  geom_tile(data = pie %>% tidyr::gather(type, value, -lon, -lat, -radius),
             aes(x = lon, y = lat, fill = type), 
             color = "black", width = 0.01, height = 0.01, 
            inherit.aes = FALSE) +

  pie.list$subgrob

plot


有没有办法设置饼图各个部分的颜色?添加scale_fill_manual()只会改变图例。 - George

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接