我正在使用 ggplot2 在 R 中制作一些靶心图。它们看起来令人愉快,每个人都非常高兴 - 除了他们希望将靶心图层的值绘制在图表上。我很乐意将它们放在情节的右下角,甚至放在情节的边缘,但我在这样做时遇到了一些困难。
这是示例数据:
critters <- structure(list(Zoo = "Omaha", Animals = 50, Bears = 10, PolarBears = 3), .Names = c("Zoo",
"Animals", "Bears", "PolarBears"), row.names = c(NA, -1L), class = "data.frame")
以及如何绘制它:
d <- data.frame(animal=factor(c(rep("Animals", critters$Animals),
rep("Bears", critters$Bears), rep("PolarBears", critters$PolarBears)),
levels = c("PolarBears", "Bears", "Animals"), ordered= TRUE))
grr <- ggplot(d, aes(x = factor(1), fill = factor(animal))) + geom_bar() +
coord_polar() + labs(x = NULL, fill = NULL) +
scale_fill_manual(values = c("firebrick2", "yellow2", "green3")) +
opts(title = paste("Animals, Bears and Polar Bears:\nOmaha Zoo", sep=""))
我想在这个情节的右下角添加一个列表,说,
Animals: 50
Bears: 10
PolarBears: 3
但我无法弄清楚如何。我到目前为止的努力
annotate()
部分受到极坐标的阻碍。如果我必须在标题中添加数字,那就这样吧——但我总是希望有一个更优雅的解决方案。提前致谢。编辑:
对后来者的重要说明:靶心是映射到极坐标的条形图。条形图的 ggplot2 默认设置是合理地堆叠它们。然而,这意味着你的靶心环也将被堆叠(例如,在我的例子中,半径等于所有三组的总和,63,而不是最大组的大小,50)。我不要认为这是大多数人对靶心图的期望,尤其是当组嵌套时。使用
geom_bar(position = position_identity())
会将堆叠的环变成分层的圆圈。编辑 2:来自 ggplot2 的示例文档:
最佳答案
您也可以将其直接添加到绘图中:
grr <- ggplot(d, aes(x = factor(1), fill = factor(animal))) + geom_bar() +
coord_polar() + labs(x = NULL, fill = NULL) +
scale_fill_manual(values = c("firebrick2", "yellow2", "green3")) +
opts(title = paste("Animals, Bears and Polar Bears:\nOmaha Zoo", sep=""))+
geom_text(y=c(3,10,50)-3,label=c("3","10","50"),size=4)
grr
关于r - R中的更多靶心绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1424189/