four-eyes
four-eyes

Reputation: 12394

Barchart for each row in dataframe

I have a dataframe and I want a barplot for each row in this dataframe.

for(i in levels(myDf$name)) {
    barplot(cbind(unlist(myDf[i, 1:2]), unlist(myDf[i, 3:4])), beside=TRUE)
}

However, that does not work. It gives me no output... How would I plot that in one window so that I can export it to a file?!

*edit:

myDf<-data.frame(name=c('xyz','ybc','def'),
              var1=c(2,8,7), 
              var2=c(1,4,5),
              var3=c(3.8,2.5,8.4),
              var4=c(93.8,42.5,91.4))

Upvotes: 1

Views: 1646

Answers (2)

Kunal Puri
Kunal Puri

Reputation: 3427

If I understood it correctly, then here is an alternative.

data <- t(myDf[,2:5])

colnames(data) <- myDf$name

barplot(data,legend.text = rownames(data),beside=T,xlab='Row', ylab='Value')

graph

Upvotes: 2

Luke Singham
Luke Singham

Reputation: 1726

for(i in seq(myDf$name)) {
    barplot(cbind(unlist(myDf[i, 1:2]), unlist(myDf[i, 3:4])), beside=TRUE)
}

Upvotes: 1

Related Questions