简体   繁体   English

绘制两个分类变量

[英]plot r two categorical variables

I am using below command to plot two categorical variables in R 我正在使用以下命令在R中绘制两个类别变量

gender has 2 levels and Income has 9 levels. 性别有2个级别,收入有9个级别。

spineplot(main$Gender,main$Income, xlab="Gender", ylab="Income levels: 1 is lowest",xaxlabels=c("Male","Female"))

It produces chart like below 它产生如下图 在此处输入图片说明

  1. How can i plot this chart in color? 我该如何用颜色绘制此图表?
  2. How can i show % of each income level within each box? 如何显示每个方框中每个收入水平的百分比? for example female income level 1 has 21% of data. 例如,女性收入1级拥有21%的数据。 How can i show 21% within the dark colored area? 如何在深色区域显示21%
################update 1 ################ update 1

Adding reproducible example 添加可复制的示例

 fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1,2,2,2,2), levels = c(1, 2), labels = c("male", "female")) gender <- factor(rep(c(1:9),3)) spineplot(fail,gender) 

I think it may be easier to do this with a barplot since spineplot doesn't return anything useful. 我认为使用barplot进行此操作可能会更容易,因为spineplot不会返回任何有用的信息。

The default would be the following, but you can adjust the widths of the bars to some other variable (you can see the x-axis coordinates are returned): 默认值是以下值,但您可以将条形的宽度调整为其他变量(可以看到返回了x轴坐标):

par(mfrow = 1:2)
(barplot(table(gender, fail)))
# [1] 0.7 1.9
(barplot(table(gender, fail), width = table(fail)))
# [1] 10.7 26.9

在此处输入图片说明

With some final touches we get 经过最后的修改,我们得到了

tbl <- table(gender, fail)
prp <- prop.table(tbl, 2L)
yat <- prp / 2 + apply(rbind(0, prp[-nrow(prp), ]), 2L, cumsum)

bp <- barplot(prp, width = table(fail), axes = FALSE, col = rainbow(nrow(prp)))

axis(2L, at = yat[, 1L], labels = levels(gender), lwd = 0)
axis(4L)

text(rep(bp, each = nrow(prp)), yat, sprintf('%0.f%%', prp * 100), col = 0)

在此处输入图片说明

Compare to 相比于

spineplot(fail, gender, col = rainbow(nlevels(gender)))

在此处输入图片说明

An alternative to the interesting solution of @rawr is: @rawr有趣的解决方案的替代方法是:

fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1,
                 1, 1, 1, 2, 1, 1, 1, 1, 1,2,2,2,2),
               levels = c(1, 2), labels = c("male", "female"))
gender <- factor(rep(c(1:9),3))

mypalette <- colorRampPalette(c("lightblue","darkblue"))
tbl <- spineplot(fail, gender, xlab="Gender", ylab="Income levels: 1 is lowest",
     xaxlabels=c("Male","Female"), col=mypalette(nlevels(gender)) )
print(tbl)

#        Income levels: 1 is lowest
# Gender   1 2 3 4 5 6 7 8 9
# male   2 1 2 1 3 2 2 2 1
# female 1 2 1 2 0 1 1 1 2

print.perc <- function(k, tbl, ndigits=2, str.pct="%") {
   # These lines of codes are the same used by from spineplot
   # for the calculation of the x-position of the stacked bars
   nx <- nrow(tbl)
   off <- 0.02
   xat <- c(0, cumsum(prop.table(margin.table(tbl, 1)) + off))
   posx <- (xat[1L:nx] + xat[2L:(nx + 1L)] - off)/2
   # Proportions by row (gender)       
   ptbl <- prop.table(tbl,1)
   # Define labels as strings with a given format
   lbl <- paste(format(round(100*ptbl[k,], ndigits), nsmall=ndigits), str.pct, sep="")
   # Print labels
   # cumsum(ptbl[k,])-ptbl[k,]/2 is the vector of y-positions
   # for the centers of each stacked bar
   text(posx[k], cumsum(ptbl[k,])-ptbl[k,]/2, lbl)
}

# Print income levels for males and females
strsPct <- c("%","%")
for (k in 1:nrow(tbl)) print.perc(k, tbl, ndigits=2, str.pct=strsPct[k])

在此处输入图片说明

Hope it can help you. 希望它能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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