Barplot in R, aggregazione dei dati campionati
Domanda
Voglio una barplot accatastati, o almeno due barplots (histogramms) dei dati sottostanti. Ma io cant't capire come. plot (on-line) non è la soluzione, Sto cercando. Si prega di vedere di seguito.
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
So che ho bisogno di un passo, in cui i dati vengono aggregati a:
online offline
sehrwichtig 6 7
unwichtig 0 1
wichtig 3 5
Ma come?
Soluzione
che l'aggregazione è solo una semplice chiamata al table
all'interno di apply
:
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
che è possibile alimentare in barplot
.
Altri suggerimenti
Con ggplot2, non è necessario pre-aggregare i dati:
library(ggplot2)
qplot(online, data = df, fill = offline)
qplot(offline, data = df, fill = online)
#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))
La risposta di Dirk è la strada da percorrere, ma i dati del OP una semplice apply(foo,2,table)
non funziona - è necessario affrontare con l'entrata 0, forse in questo modo:
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
Per l'amor di brevità, è possibile combinare gli ultimi 3 linee:
tallies <- apply(df,2,function(x){y <- table(x)[df.labels];
names(y) <- df.labels; y[is.na(y)] <- 0; y})
L'output è:
> tallies
online offline
sehrwichtig 10 4
wichtig 4 4
unwichtig 0 6
Non ho fatto io stesso, ma so del pacchetto R che un sacco di gente usa per il passo di metterlo in secondo array lì. Si chiama reshape
:
http://www.statmethods.net/management/reshape.html
http://had.co.nz/reshape/introduction.pdf
Per quanto riguarda la parte tracciare, penso che lattice
o ggplot
probabilmente entrambi hanno funzioni per fare esattamente quello che vuoi, ma ancora una volta io sono un newbie R quindi non posso dire molto di più ...