R 中的更多靶心绘图
我正在使用 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 文档的示例:
I'm using ggplot2 to make some bullseye charts in R. They look delightful, and everyone is very pleased - except that they'd like to have the values of the bullseye layers plotted on the chart. I'd be happy just to put them in the lower-right corner of the plot, or even in the plot margins, but I'm having some difficulty doing this.
Here's the example data again:
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")
And how to plot it:
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=""))
I'd like to add a list to, say, the lower right corner of this plot saying,
Animals: 50
Bears: 10
PolarBears: 3
But I can't figure out how. My efforts so far with annotate()
have been thwarted, in part by the polar coordinates. If I have to add the numbers to the title, so be it - but I always hold out hope for a more elegant solution.
EDIT:
An important note for those who come after: the bullseye is a bar plot mapped to polar coordinates. The ggplot2 default for bar plots is, sensibly, to stack them. However, that means that the rings of your bullseye will also be stacked (e.g. the radius in my example equals the sum of all three groups, 63, instead of the size of the largest group, 50). I don't think that's what most people expect from a bullseye plot, especially when the groups are nested. Using geom_bar(position = position_identity())
will turn the stacked rings into layered circles.
EDIT 2: Example from ggplot2 docs:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也可以将其直接添加到绘图中:
you can also add it directly to the plot:
您可以将数字添加到图例中。
You could add the numbers to the legend.