1

I have about 10 categorical variables - pay1, pay2, ... , pay10 each having values either 'Yes' or 'No'. I would like to plot the count of each of these variables on a graph. For example - bar1 on the chart should refer to the variable 'pay1' reflecting the total number of observations divided between 'Yes' and 'No'('Yes' on top of 'No' or vice versa) This scheme should be consistent with all the 10 variables on the chart. If I am able to display the percentage of 'Yes' and 'No' for each bar, even better. Would someone be able to help out on this?

TIA.

Patthebug
  • 4,647
  • 11
  • 50
  • 91
  • 5
    Please post what you've tried to solve the problem. – John Sep 16 '13 at 01:48
  • 4
    Also make your [question reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jilber Urbina Sep 16 '13 at 01:59
  • Here is what I tried to do earlier: ggplot(Paid_For_Online_Content, aes(x=interaction(pay1a, pay1b), y='Count', fill='Type')) + geom_bar(position='stack', stat='identity') but this did not solve the problem. – Patthebug Sep 16 '13 at 03:59
  • Possible duplicate of [Plot two graphs in same plot in R](https://stackoverflow.com/questions/2564258/plot-two-graphs-in-same-plot-in-r) – divibisan Jul 08 '19 at 22:33

2 Answers2

1

Edit Like this?

set.seed(1) # make reproducible
### 3x variables, 5x observations
df1 <- data.frame(x1=sample(c("yes","no"),5, replace=TRUE),
                  x2=sample(c("yes","no"),5, replace=TRUE),
                  x3=sample(c("yes","no"),5, replace=TRUE)
                  )
library(reshape2)
### convert to 'long form'
m1 <- melt(df1, measure.vars=c("x1","x2","x3"))
### now use facets to give one plot per variable
library(ggplot2)
qplot(variable, data=m1, fill=value) + facet_wrap( facets= ~variable, scale="free_x")

giving:

enter image description here

Or if you want the 'yes/no's side-by-side, which looks nicer to me:

qplot(value, data=m1, fill=value) + facet_wrap( facets= ~variable, scale="free_x")
dardisco
  • 5,086
  • 2
  • 39
  • 54
  • 1
    To get around possible `NA` values you might be better off using something like: `barplot(prop.table(sapply(df1,table),2)["yes",])` - though this question is also shooting for a `ggplot` solution I believe. – thelatemail Sep 16 '13 at 03:34
  • Thanks for the reply, but I want my bars to clearly distinguish between 'Yes' and 'No'. Different colors for 'Yes' and 'No' in the same bar would be great too. – Patthebug Sep 16 '13 at 04:05
0

Using the data frame generated in the other answer, how about this? I think you have to be fairly specific about how you want your x-axis structured to get a useful answer here.

set.seed(1) # make reproducible
### 3x variables, 5x observations
df1 <- data.frame(x1=sample(c("yes","no"),5, replace=TRUE),
              x2=sample(c("yes","no"),5, replace=TRUE),
              x3=sample(c("yes","no"),5, replace=TRUE)
              )
library(reshape2)
m1 <- melt(df1, measure.vars=c("x1","x2","x3"))
m1[,"varval"]<-paste(m1$variable, m1$value, sep="-")

library(ggplot2)
# All counts now have a common x-axis:
varp<-ggplot(m1, aes(varv, fill=value))+geom_bar(stat="bin")
varp

samplefrequencyplot.jpeg

rebeccmeister
  • 300
  • 2
  • 12
  • 1
    Your point about needing more information about the x-axis structure is spot on. But, it seems to me that this layout loses a lot of meaning, compared to stacking them as dardisco did. – Andy Clifton Feb 19 '14 at 04:34