2009-09-14 63 views
6

我使用ggplot2在R中制作一些靶心图。它们看起来令人愉快,每个人都非常高兴 - 除了他们想要在图表上绘制靶心层的值。我会很高兴把他们放在情节的右下角,甚至是在情节边缘,但是我在做这件事时遇到了一些困难。更多的靶心在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文档:
enter image description here

+0

没必要道歉在问你的问题。好的问题和我学到的东西阅读答案。 – 2009-09-15 19:53:07

+1

主要是为绘制舷窗而抱歉,特别是在系列中。虱子比较容易(至少对我来说)。 – 2009-09-15 20:51:23

回答

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="")) 
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 
+0

两个很好的答案!我刚刚给了其他最好的答案,因为那是我去的路线。 – 2009-09-15 02:06:55