简体   繁体   中英

How do I plot a number of categorical variables on a graph in R?

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.

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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM