饼图上的文本重叠问题

7

我试图修复这个重叠的标签:

我的代码:

  values=c(164241,179670)
  labels=c("Private", "Public")
  colors=c("#cccccc", "#aaaaaa")
  categoriesName="Access"
  percent_str <- paste(round(graph$values / sum(graph$values) * 100,1), "%", sep="")

  values <- data.frame(val = graph$values, Type = graph$labels, percent=percent_str )

  pie <- ggplot(values, aes(x = "", y = val, fill = Type)) + geom_bar(width = 1) + 
          geom_text(aes(y = **val + 1**, **hjust=0.5**, **vjust=-0.5**, label = percent), colour="#333333", face="bold", size=10) +
          coord_polar(theta = "y") + ylab(NULL) + xlab(NULL) +
          scale_fill_manual(values = graph$colors) + labs(fill = graph$categoriesName) +
          opts( title = graph$title, 
                axis.text.x = NULL,
                plot.margin = unit(c(0,0,0,0), "lines"), 
                plot.title = theme_text(face="bold", size=14), 
                panel.background = theme_rect(fill = "white", colour = NA) )
  print(pie)

我尝试更改带有星号(** **)标记的值,但没有任何进展。非常感谢您提供的任何帮助。

1个回答

15

这里是一个例子:

pie <- ggplot(values, aes(x = "", y = val, fill = Type)) + 
  geom_bar(width = 1) + 
  geom_text(aes(y = val/2 + c(0, cumsum(val)[-length(val)]), label = percent), size=10)
pie + coord_polar(theta = "y")

在此输入图片描述

或许这会帮助你理解它的工作方式:

pie + coord_polar(theta = "y") + 
  geom_text(aes(y = seq(1, sum(values$val), length = 10), label = letters[1:10]))

在此输入图片描述


你知道如何将它们放在更外面吗?增加文本半径。 - fabiopedrosa
尝试使用 geom_text(aes(x = 1.3, y = ... - kohske
我一直以为我理解了你的逻辑,直到我尝试了一个更复杂的例子:D 这是结果:http://i.imgur.com/E5rsC.png 对于这些值:c(81805,78424,19441),标签为c("Singapore", "Unknown", "Other"),y= val/2 + c(0, cumsum(val)[-length(val)]),因为它有意义。有什么建议吗? - fabiopedrosa
啊,应该是这样的:y = sum(val)-(val/2 + c(0, cumsum(val)[-length(val)])) - kohske
是的,就是这样。谢谢!最后只有一个小问题……有没有办法将标签大小映射到数值上?我需要在较小的饼图中使用较小的文字。 - fabiopedrosa
实际上,我不确定它是否起作用了。我们可以看到大小可能是成比例的。我尝试使用类似于5+百分比*5的公式来定义大小,但没有成功。此外,右侧的分组不应该发生。 - fabiopedrosa

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