饼图把文本放在彼此的上面。

时间:2022-03-02 17:31:53

Just trying to fix this overlapped labeling:

试着修正这个重叠的标签:

饼图把文本放在彼此的上面。

My code:

我的代码:

  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)

Tried messing with the values marked with asterisks (** **) but haven't got anywhere. Any help appreciated.

尝试使用星号(** ** *)标记的值,但是没有得到任何结果。任何帮助表示赞赏。

1 个解决方案

#1


14  

here is an example:

这是一个例子:

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")

饼图把文本放在彼此的上面。

Perhaps this will help you understand how it work:

也许这将帮助你理解它是如何工作的:

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

饼图把文本放在彼此的上面。

#1


14  

here is an example:

这是一个例子:

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")

饼图把文本放在彼此的上面。

Perhaps this will help you understand how it work:

也许这将帮助你理解它是如何工作的:

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

饼图把文本放在彼此的上面。