簡體   English   中英

ggplot2 - 餅圖 - 以相反順序排列的值標簽

[英]ggplot2 - piechart - value labels in reverse order

我正在嘗試將標簽與我的餅圖與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")

輸出: MyOutput中

我讀了這些主題,但沒有成功:

從ggplot2 2.2.0開始,您可以使用position_stackvjust = .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")

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM