2009-09-14 64 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