0
votes

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

1
Are you open to other plotting library like ggplot2 or lattice or should it be a base R barplot? - talat
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. - dju
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)") - MrFlick
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. - dju

1 Answers

1
votes

Try:

 barplot(t(propVars),beside=T)

enter image description here