问题
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.
回答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:

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")
回答2:
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

来源:https://stackoverflow.com/questions/18819274/how-do-i-plot-a-number-of-categorical-variables-on-a-graph-in-r