2010-03-08 4 views
1

Je veux un diagramme à barres empilé, ou au moins deux graphiques à barres (histogrammes) des données ci-dessous. Mais je ne peux pas comprendre comment. intrigue (en ligne) n'est pas la solution, je cherche. S'il vous plaît voir ci-dessous.Barplot dans R, agrégation des données échantillonnées

  online    offline 
1   sehrwichtig    wichtig 
2    wichtig   unwichtig 
3   sehrwichtig   unwichtig 
4   sehrwichtig   sehrwichtig 
5   sehrwichtig   sehrwichtig 
6   sehrwichtig   unwichtig 
7   sehrwichtig   unwichtig 
8    wichtig    wichtig 
9    wichtig   unwichtig 
10  sehrwichtig   sehrwichtig 
11  sehrwichtig    wichtig 
12  sehrwichtig   unwichtig 
13   wichtig   sehrwichtig 
14  sehrwichtig    wichtig 

Je sais que je dois une étape, où les données sont agrégées à:

    online  offline 
    sehrwichtig   6   7 
    unwichtig    0   1 
    wichtig    3   5 

Mais comment?

Répondre

3

Cette agrégation est juste un simple appel à l'intérieur de tableapply:

R> foo <- data.frame(online=sample(c("S","W","U"),10,TRUE), 
        offline=sample(c("S","W","U"),10,TRUE)) 
R> apply(foo,2,table) 
    online offline 
S  3  1 
U  4  5 
W  3  4 

que vous pouvez alimenter en barplot.

+1

J'ai accepté ces solutions pour des raisons de simplicité. Merci à tous les autres. J'ai beaucoup appris. – Felix

0

Je ne l'ai pas fait moi-même, mais je connais le paquet R que beaucoup de gens utilisent pour l'étape de le mettre dans le deuxième tableau. Il est appelé reshape:

http://www.statmethods.net/management/reshape.html

http://had.co.nz/reshape/introduction.pdf

En ce qui concerne la partie de traçage, je pense que lattice ou ggplot probablement les deux ont des fonctions pour faire exactement ce que vous voulez, mais encore une fois je suis un débutant R donc je ne peut pas dire beaucoup plus ...

+0

Merci pour la http://www.statmethods.net/ page. – Felix

1

La réponse de Dirk est la voie à suivre, mais les données de l'OP un apply(foo,2,table) simple, ne fonctionnera pas - vous devez faire face à la peut-être l'entrée 0, comme ceci:

my.data <- "online    offline 
1   sehrwichtig    wichtig 
2    wichtig   unwichtig 
3   sehrwichtig   unwichtig 
4   sehrwichtig   sehrwichtig 
5   sehrwichtig   sehrwichtig 
6   sehrwichtig   unwichtig 
7   sehrwichtig   unwichtig 
8    wichtig    wichtig 
9    wichtig   unwichtig 
10  sehrwichtig   sehrwichtig 
11  sehrwichtig    wichtig 
12  sehrwichtig   unwichtig 
13   wichtig   sehrwichtig 
14  sehrwichtig    wichtig" 

df <- read.table(textConnection(my.data)) 

df.labels <- unique(as.character(apply(df,2,as.character))) 
tallies <- apply(df,2,function(x)table(x)[df.labels]) 
tallies[is.na(tallies)] <- 0 
rownames(tallies) <- df.labels 

Par souci de concision, vous pouvez combiner les 3 dernières lignes:

tallies <- apply(df,2,function(x){y <- table(x)[df.labels]; 
            names(y) <- df.labels; y[is.na(y)] <- 0; y}) 

La sortie est:

> tallies 
      online offline 
sehrwichtig  10  4 
wichtig   4  4 
unwichtig  0  6 
+0

Eh bien, j'avais besoin de gérer 0 entrées. Merci pour le conseil. – Felix

2
#generate data 
df<- read.table(textConnection(' 
     online    offline 
sehrwichtig    wichtig 
    wichtig   unwichtig 
sehrwichtig   unwichtig 
sehrwichtig   sehrwichtig 
sehrwichtig   sehrwichtig 
sehrwichtig   unwichtig 
sehrwichtig   unwichtig 
    wichtig    wichtig 
    wichtig   unwichtig 
sehrwichtig   sehrwichtig 
sehrwichtig    wichtig 
sehrwichtig   unwichtig 
    wichtig   sehrwichtig 
sehrwichtig    wichtig' 
),header=T) 

#factor levels should be the same 
levels(df$online) <- levels(df$offline) 

my_table <- t(aaply(df,2,table)) 

library(graphics) 
barplot2(my_table,legend = rownames(my_table), ylim = c(0, 20)) 

alt text

3

Avec ggplot2, vous n'avez pas besoin d'effectuer une pré-agréger les données:

library(ggplot2) 
qplot(online, data = df, fill = offline) 
qplot(offline, data = df, fill = online)