我正在尝试将标签与我的饼图与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")
我阅读了这些主题,但没有取得任何成功:
答案 0 :(得分:6)
从ggplot2 2.2.0开始,您可以使用vjust = .5
和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")
将标签置于堆积条形图(以及饼图)中心。您不再需要计算ggplot2之外的位置。有关这些更改的详细信息,请参阅NEWS。
{{1}}