0

I got a Data Frame like this:

data <- data.frame(Sex = c("female", "male", "male", "female"),
                  A1 = c(T,T,F,F),
                  A2 = c(T,F,F,F),
                  A3 = c(T,T,T,F)) 

And I want a single Barplot which shows me the frequency of Trues of A1, A2 and A3 grouped by females and males.

I tried:

# table: 
a <- round(prop.table(table(data$Sex, data$A1))*100, 4)
b <- round(prop.table(table(data$Sex, data$A2))*100, 4)
c <- round(prop.table(table(data$Sex, data$A3))*100, 4)

propVars <- cbind(a, b, c)

# remove false:
propVars <- propVars[,c(2,4,6)]

# plot:
barplot(propVars,beside = T)

But now I got the Trues on the x-axis an the sexes as different bars. But i like to have the females on the left side of the x-axis and the males on the right side of the x-axis. And for A1,A2 and A3 I want one bar each, showing the frequency of the trues at every side, one for the females and one for the males. I hope you understand :)

Thanks

4
  • Are you open to other plotting library like ggplot2 or lattice or should it be a base R barplot? Commented Jul 22, 2014 at 12:50
  • should be base R barplot if possible. I got some other plots made with it and don't want change the look. But if its very difficult with r barplot i will be happy with a ggplot2 or lattice solution too. Commented Jul 22, 2014 at 12:53
  • 1
    are you sure this is the code you are running? I get complaints at the cbind step ("number of rows of matrices must match (see arg 3)") Commented Jul 22, 2014 at 13:03
  • sorry.. you're right. it's not my actual data frame i'm working with. I changed it to a much smaller one. But now i edited the example data frame in my question and now it should work. Commented Jul 22, 2014 at 13:11

1 Answer 1

1

Try:

 barplot(t(propVars),beside=T)

enter image description here

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

Comments

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.