【问题标题】:Pie plot getting its text on top of each other饼图将其文本置于彼此之上
【发布时间】:2012-02-15 15:33:21
【问题描述】:

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

我的代码:

  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)

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

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这是一个例子:

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

    【讨论】:

    • 你知道如何将它们更多地放在外面吗?增加文本半径。
    • 试试geom_text(aes(x = 1.3, y = ...
    • 我真的以为我理解了你的逻辑,直到我尝试了一个更复杂的例子:D 这是结果:i.imgur.com/E5rsC.png 对于这个值:c(81805,78424,19441),标签 c("Singapore ", "Unknown", "Other") 和 y = val/2 + c(0, cumsum(val)[-length(val)]) 因为它是有意义的。任何指针?
    • 啊,应该是这样的:y = sum(val)-(val/2 + c(0, cumsum(val)[-length(val)]))
    • 是的,就是这样。谢谢!只是一个小问题要完成......有没有办法将标签大小映射到值?我需要一个小馅饼中的小文本。
    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多