我正在尝试将标签与我的饼图与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")
输出:
我读了这些主题,但没有成功:
ggplot,facet,piechart:将文本放在饼图切片的中间
R + ggplot2 =>在构面饼图上添加标签
aosmith.. 6
从ggplot2 2.2.0开始,您可以使用position_stack
with 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")
从ggplot2 2.2.0开始,您可以使用position_stack
with 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")