1

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))
4
  • Can you please give a glimpse of dataset by head() or dput()? Commented Apr 30, 2016 at 11:14
  • @KunalPuri added some data Commented Apr 30, 2016 at 11:40
  • Do any warning/error messages get printed to the console? Commented Apr 30, 2016 at 12:06
  • @LukeSingham no, nothing. The plot window simply stays empty. Commented Apr 30, 2016 at 12:13

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

same as with my code. The plot window stays empty!+
Interesting, I get errors when using levels, using seq fixes the problem for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.