2014-12-14 10 views
5

Próbuję wykreślić tabelę za pomocą barplota i dodając do niego wartości.Dodawanie wartości do wykresu z tabeli w R

tt = structure(c(7L, 13L, 24L, 30L, 30L, 38L, 35L, 45L, 37L, 
43L, 38L, 59L, 33L, 45L, 37L, 58L), .Dim = c(2L, 8L), .Dimnames = structure(list(param = c("A", 
"B"), xvar = c("5", "6", "7", "8", "9", "10", "11", "12")), .Names = c("param", "xvar")), class = "table") 
tt 
    xvar 
param 5 6 7 8 9 10 11 12 
    A 7 24 30 35 37 38 33 37 
    B 13 30 38 45 43 59 45 58 

bb= barplot(tt) 
text(bb, 0, tt) 

enter image description here

I:

bb= barplot(tt) 
text(bb, tt, tt) 

enter image description here

Obie nie umieścić wartości prawidłowo. Próbowałem również t (tt) w tekście(), ale nie działa poprawnie. Jak to zrobić? Dzięki za pomoc.

Odpowiedz

9

Cóż, to działa, ale muszę, że istnieje lepszy sposób ...

bb <- barplot(tt, col=c("grey60","grey80")) 
text(bb,tt[1,]-4,labels=tt[1,],cex=.8) 
text(bb,colSums(tt)-4,labels=tt[2,],cex=0.8) 

A oto rozwiązanie ggplot, tylko dla kompletności.

library(ggplot2) 
ggplot(as.data.frame(tt),aes(x=factor(xvar),y=Freq,fill=param)) + 
    geom_bar(stat="identity",position="stack")+ 
    geom_text(aes(label=Freq),position="stack",vjust=1)+ 
    scale_fill_manual(values=c("grey60","grey80"))+ 
    theme_bw() 

+0

Szczególnie podoba mi się wersja ggplot. Dzięki. – rnso

Powiązane problemy