17

In R I have a data.frame like the one on the top of the picture.

Is there a possibility to create a barplot like below?

data.frame:

      X1   X2   X3  
 --- ---- ---- ---- 
  A    2   3    4   
  B    4   2    1   
  C    1   NA   NA  

Barplot:

----------------------------
|                          |
|       #    #             |
|    #  #    #             |
| #  #  #    #  #          |
| #  #  #    #  #  #    #  |
----------------------------
  X1 X2 X3   X1 X2 X3   x1
     A           B       C
3
  • If you mean an ascii output try the txtplot package. Commented May 17, 2013 at 20:35
  • Sorry, this was just an example ;) i want to have a plot like the one i get with the barplot function Commented May 17, 2013 at 20:38
  • Probably you want to change the titel of your question? You actually want to create a barplot from a data.frame and not vice versa. Commented May 17, 2013 at 20:45

2 Answers 2

31

Using base graphics you can do this simply:

mydf <- data.frame( X1=c(A=2, B=4, C=1), X2=c(3,2,NA), X3=c(4,1,NA) )
barplot(t(as.matrix(mydf)), beside=TRUE)

Using additional calls to axis can give the labeling more like in the question.

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

3 Comments

this should be the accepted answer - why use two libraries to do such a simple task?
because converting to a matrix and then a table could add a lot of overhead if you have a large df?
@wordsforthewise, if the df is so large that this overhead is even noticible (let alone a problem) then a barplot is probably not the best visualization of it anyways.
20

Assuming, that you don't want ascii output, here is a solution using ggplot2:

# load / generate your data
mydf <- data.frame( X1 = c(2,4,1), X2 = c(3,2,NA), x3 = c(4,1,NA), row.names=c("A","B","C") )
mydf$Category  <- row.names(mydf)

# bring your data to long format as needed by ggplot
library(reshape2)
mydf.molten <- melt(mydf, value.name="Count", variable.name="Variable", na.rm=TRUE)

# plot and facet by categories
library(ggplot2)
qplot( data=mydf.molten, x = Variable, y = Count, geom="bar", stat = "identity" ) + facet_wrap( "Category" )

enter image description here

For further details, I'd recommend to consult the ggplot2 manual, especially the chapter about geom_bar and facet_wrap.

2 Comments

got this error with your code: > mydf.molten <- melt(mydf, value.name="Count", variable.name="Variable", na.rm=TRUE) Using Category as id variables > qplot( data=mydf.molten, x = Variable, y = Count, geom="bar", stat = "identity" ) + facet_wrap( "Category" ) Error: stat_count() must not be used with a y aesthetic. In addition: Warning message: stat` is deprecated `
@Mona Jalal; ggplot2 has changed since 2013, especially when it upgraded from ver. 1.xxx to 2.xxx.

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.