在R中更多的靶心绘图

Mat*_*ker 6 plot r ggplot2

我正在使用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")
Run Code Online (Sandbox Code Playgroud)

以及如何绘制它:

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=""))
Run Code Online (Sandbox Code Playgroud)

我想在这个剧情的右下角添加一个列表,

Animals: 50
Bears: 10
PolarBears: 3
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何.到目前为止,我的努力annotate()受到了部分极坐标的阻碍.如果我必须在标题中添加数字,那就这样吧 - 但我总是希望有一个更优雅的解决方案.提前致谢.

编辑:对于那些追随者的重要说明:靶心是映射到极坐标的条形图.理论上,条形图的ggplot2默认值是堆叠它们.但是,这意味着你的靶心的环也将被叠加(例如,我的例子中的半径等于所有三个组的总和,63,而不是最大组的大小,50).我认为这是大多数人对靶心情节的期望,特别是当群体嵌套时.使用geom_bar(position = position_identity())会将堆叠的环转变为分层圆圈.

编辑2:ggplot2文档的示例:
在此输入图像描述

Ian*_*ows 5

你也可以直接将它添加到情节中:

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
Run Code Online (Sandbox Code Playgroud)


Thi*_*rry 4

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

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=""))
Run Code Online (Sandbox Code Playgroud)