R 中的更多靶心绘图

发布于 2024-08-04 22:28:07 字数 1482 浏览 1 评论 0原文

我正在使用 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:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷又爱笑 2024-08-11 22:28:07

您也可以将其直接添加到绘图中:

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

you can also add it directly to the plot:

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
酷到爆炸 2024-08-11 22:28:07

您可以将数字添加到图例中。

library(ggplot2)
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))
levels(d$animal) <- apply(data.frame(table(d$animal)), 1, paste, collapse = ": ")
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=""))

You could add the numbers to the legend.

library(ggplot2)
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))
levels(d$animal) <- apply(data.frame(table(d$animal)), 1, paste, collapse = ": ")
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=""))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文