[英]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")
我读了这些主题,但没有成功:
从ggplot2 2.2.0开始,您可以使用position_stack
和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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.