ggplot2 - 饼图 - 以相反顺序排列的值标签

sta*_*dav 3 r ggplot2

我正在尝试将标签与我的饼图与ggplot2匹配:

码:

values=c(59,4,4,11,26)
labels=c("catA", "catB","catC","catD","catE")
pos = cumsum(values)- values/2
graph <- data.frame(values, labels,pos)

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, pos = graph$pos )

pie <- ggplot(values, aes(x = "", y = val, fill = Type)) + 
  geom_bar(width = 1,stat="identity") + 
  geom_text(aes(x= "", y=pos, label = val), size=3) 
pie + coord_polar(theta = "y")
Run Code Online (Sandbox Code Playgroud)

输出: MyOutput中

我读了这些主题,但没有成功:

aos*_*ith 6

从ggplot2 2.2.0开始,您可以使用position_stackwith vjust = .5来堆叠条形图中的标签(以及饼图).您不再需要计算ggplot2之外的位置.有关这些更改的更多详细信息,请参阅新闻.

ggplot(values, aes(x = "", y = val, fill = Type)) + 
    geom_bar(width = 1,stat="identity") + 
    geom_text(aes(label = val), size=3, position = position_stack(vjust = 0.5))  + 
    coord_polar(theta = "y")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述