ggmap 上的 ggplot 饼图:标签会破坏小图

ggplot piecharts on a ggmap: labels destroy the small plots

我想要带有标签的小饼图的 ggmap 是使用以下代码生成的:

p <-
  get_googlemap(
    "Poland",
    maptype = "roadmap",
    zoom = 6,
    color = "bw",
    crop = T,
    style = 'feature:all|element:labels|visibility:off'  #'feature:administrative.country|element:labels|visibility:off' or 'feature:all|element:labels|visibility:off'
  ) %>% 
  ggmap() + coord_cartesian() +
  scale_x_continuous(limits = c(14, 24.3), expand = c(0, 0)) +
  scale_y_continuous(limits = c(48.8, 55.5), expand = c(0, 0))

我正在尝试按照答案在 ggmap 上绘制我的小 ggplot 饼图

我准备数据如下:

df <-
  df %>%  mutate(Ours = Potential  * MS, Others = Potential - Ours) %>%
  na.omit() %>% filter(Potential > 0) %>%
  select(-L.p., -MS) %>%
  group_by(Miasto) %>%
  summarise_each_(vars = c("Potential", "Ours", "Others"),
                  funs = funs(Sum = "sum")) %>%
  left_join(coordinatesTowns, by = c("Miasto" = "address")) %>% 
  distinct(Miasto, .keep_all = T) %>% 
  select(-X) %>% ungroup()

df <-df  %>%  gather(key=component, value=sales, c(Ours_Sum,Others_Sum)) %>% 
  group_by(lon, lat,Potential_Sum)

我的数据看起来像

tibble::tribble(
       ~Miasto, ~Potential_Sum,     ~lon,     ~lat,    ~component, ~sales,
   "Bialystok",            100, 23.16433, 53.13333,    "Ours_Sum",     70,
   "Bialystok",            100, 23.16433, 53.13333,  "Others_Sum",     30,
   "Bydgoszcz",             70, 18.00762,  53.1235,    "Ours_Sum",      0,
   "Bydgoszcz",             70, 18.00762,  53.1235,  "Others_Sum",     70,
      "Gdansk",             50, 18.64637, 54.35205,    "Ours_Sum",     25,
      "Gdansk",             50, 18.64637, 54.35205,  "Others_Sum",     75,
    "Katowice",             60, 19.02754, 50.25842,    "Ours_Sum",     20,
    "Katowice",             60, 19.02754, 50.25842,  "Others_Sum",     40
  )

最后一行 group_by 对于生成要粘贴到我的地图中的地块至关重要。 (我怀疑这可能是我下面描述的问题的原因)。

我想在饼图中为每个份额提供标签而不是总数

在这个答案中,我找到了应该向饼图添加标签的语法

下面是我脚本中带有 geom_text 的行(用散列注释)的语法,如果未注释,会导致我的地块消失,所有小地块的长列表(16 个条目),警告:

1: Removed 1 rows containing missing values (geom_col).

我认为原因可能在准备数据的最后一行,将其分组以进行绘图。

我用散列标记的那一行是个问题。如果我放置的散列图是正确的,如果我包含它,试图在切片上获得所需的标签,图就会消失或者是非常窄的垂直切片。

       df.grobs <- df %>% 
      do(subplots = ggplot(., aes(1, sales, fill = component)) + 
           geom_bar(position = "fill", alpha = 0.5, colour = "white", stat="identity") + 
#          geom_text( aes(label = round(sales), y=sales), position = position_stack(vjust = 0.5), size = 2.5)  +
           coord_polar(theta = "y") + 
           scale_fill_manual(values = c("green", "red"))+
           theme_void()+ guides(fill = F)) %>% 
      mutate(subgrobs = list(annotation_custom(ggplotGrob(subplots), 
                                               x = lon-Potential_Sum/300, y = lat-Potential_Sum/300, 
                                               xmax = lon+Potential_Sum/300, ymax = lat+Potential_Sum/300))) 

    df.grobs


    df.grobs %>%
    {p + 
        .$subgrobs + 
                geom_col(data = df,
                 aes(0,0, fill = component), 
                 colour = "white")+ geom_text(data=df, aes(label = Miasto),nudge_y = -0.15, size=2.5)}

为什么用散列标记的行(如果未注释)会破坏情节而不是添加标签?它似乎完全重新定义了美学。

编辑:我修改了标记行,现在 label=salesy=sales。现在,如果我注释该行,就会生成图,如果我取消注释,则会在正确的位置生成标签,但没有图。为什么我不能两者兼得?

简答:

我认为问题实际上出在您前面的行中:

geom_bar(position = "fill", alpha = 0.5, colour = "white", stat="identity") +

如果您将位置从 fill 更改为 stack(即默认值),它应该可以正常工作(至少在我的位置上是这样)。

冗长的解释:

让我们使用 mtcars 数据集的汇总版本来重现问题:

dfm <- mtcars %>% group_by(cyl) %>% summarise(disp = mean(disp)) %>% ungroup()

# correct pie chart
ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "stack") + 
  geom_text(position = position_stack(vjust = 0.5)) + 
  coord_polar(theta = "y") + theme_void()

# "empty" pie chart
ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_text(position = position_stack(vjust = 0.5)) + 
  coord_polar(theta = "y") + theme_void()

为什么改变 geom_bar 的位置会影响这个?如果我们看一下 coord_polar 步骤之前的情节,事情可能会变得更清楚:

ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "stack") + 
  geom_text(position = position_stack(vjust = 0.5))

检查条形图的 y 轴。条形图和标签位置正确。

现在position = "fill"的版本:

ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_text(position = position_stack(vjust = 0.5))

您的条形图现在占据 y 轴上的 0-1 范围,而您的标签继续占据原来的整个范围,这个范围要大得多。因此,当您将图表转换为极坐标时,条形图被挤压成一个几乎看不见的小片。