2017-01-17 140 views
2

值標籤我試圖用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") 

輸出: myoutput

我讀到這些主題,但沒有任何成功:

回答

5

在GGPLOT2 2.2.0開始,你可以使用position_stackvjust = .5在堆疊條形圖(等餅圖)到中心的標籤。您不再需要計算ggplot2之外的位置。有關這些更改的更多詳細信息,請參閱NEWS

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

enter image description here