如何在R中的图形上绘制多个分类变量?

时间:2022-09-08 14:57:48

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?

我有大约10个分类变量 - pay1,pay2,...,pay10,每个变量的值为'Yes'或'No'。我想在图表上绘制每个变量的计数。例如 - 图表上的bar1应该引用变量'pay1',反映在“是”和“否”之间划分的观察总数(“否”之上的“是”,反之亦然)此方案应与图表上的所有10个变量。如果我能够为每个条形显示“是”和“否”的百分比,那就更好了。有人能帮忙解决这个问题吗?

TIA.

TIA。

2 个解决方案

#1


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:

赠送:

如何在R中的图形上绘制多个分类变量?

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


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.

使用其他答案中生成的数据框,这个怎么样?我认为你必须非常具体地说明你希望你的x轴结构在这里得到一个有用的答案。

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

如何在R中的图形上绘制多个分类变量?

#1


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:

赠送:

如何在R中的图形上绘制多个分类变量?

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


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.

使用其他答案中生成的数据框,这个怎么样?我认为你必须非常具体地说明你希望你的x轴结构在这里得到一个有用的答案。

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

如何在R中的图形上绘制多个分类变量?