ggplot2 饼图标签中的意外行为 [英] Unexpected behaviour in ggplot2 pie chart labeling

查看:133
本文介绍了ggplot2 饼图标签中的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里检查了其他问题,但我看不到这个问题.我有标签问题.奇怪的是,除了一个标签外,所有标签的代码都可以正常工作.当我检查数据集时(这是非常简单的事情),一切似乎都很好(一列包含因子变量,另一列包含数值).

I have checked the other questions in here but I couldn't see this problem. I have a labeling problem. Weird thing is that code is working quite OK for all labels except for one. When I checked the data set (which is something really simple), everything seems quite fine (one column with factor variables, another with numerical).

这很奇怪,因为它适用于具有相同结构的其他一些数据.但是,我尝试/检查了所有内容,但无法解决此问题.问题来了:

It is weird because it works OK for some other data with the same structure. However, I tried/checked everything but couldn't solve this issue. Here is the problem:

library(ggplot2)
library(ggrepel)

df = data.frame(
  status = c("Oak", "maple", "walnut", "Pine"),
  value = c( 47.54, 37.70, 11.48, 3.28))

ggplot(df, aes(x = "" , y = value, fill = fct_inorder(status))) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y", start = 0 ) +
  scale_fill_brewer(palette = "Set3", direction = -4) +
  geom_label_repel(aes(label = paste0(value, "%")), size=4, show.legend = F, nudge_x = 1) +
  guides(fill = guide_legend(title = "Status")) +
  theme_void()

如果我至少有尝试或解释这种奇怪行为的建议,那就太好了.

It would be great if I have at least a suggestion to try or explanation of this weird behaviour.

显然,通过新的 ggplot2 更新,他们在没有提供任何额外位置数据的情况下解决了位置问题,但不知何故,如果由于技术限制而无法使用它,这可能有助于解决此类问题.

Apparently, with the new ggplot2 update they figured the position problem out without giving any extra position data but somehow, If you are unable to use it because of technical limitations, this might help to solve this kind of issues.

推荐答案

我认为问题在于 geom_bar(或更好的 geom_col)默认为 position= stackgeom_text_repel 没有.将 geom_text_repel 设置为 position="stack" 会将标签放在饼图每个部分的末尾而不是中点.

I think the problem is that the geom_bar (or better geom_col) defaults to position = stack whereas geom_text_repel doesn't. Setting geom_text_repel to position= "stack" puts the labels at the end of each section of the pie rather than the midpoint.

可以预先计算位置.下面的代码适用于显示的数据,但可能不通用,因为它取决于行的顺序.

It is possible to pre-calculate the positions. The code below works for the data shown, but might not be general as it depends on the order of the rows.

library(ggplot2)
library(ggrepel)

df = data.frame(
  status = c("Oak", "maple", "walnut", "Pine"),
  value = c( 47.54, 37.70, 11.48, 3.28))

df2 <- df %>% 
  mutate(
    cs = rev(cumsum(rev(value))), 
    pos = value/2 + lead(cs, 1),
    pos = if_else(is.na(pos), value/2, pos))

ggplot(df, aes(x = "" , y = value, fill = fct_inorder(status))) +
  geom_col(width = 1) +
  coord_polar(theta = "y", start = 0 ) +
  scale_fill_brewer(palette = "Set3", direction = -4) +
  geom_label_repel(aes(y = pos, label = paste0(value, "%")), data = df2, size=4, show.legend = F, nudge_x = 1) +
  guides(fill = guide_legend(title = "Status")) +
  theme_void()

这篇关于ggplot2 饼图标签中的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆