簡體   English   中英

繪制兩個分類變量

[英]plot r two categorical variables

我正在使用以下命令在R中繪制兩個類別變量

性別有2個級別,收入有9個級別。

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

它產生如下圖 在此處輸入圖片說明

  1. 我該如何用顏色繪制此圖表?
  2. 如何顯示每個方框中每個收入水平的百分比? 例如,女性收入1級擁有21%的數據。 如何在深色區域顯示21%
################ update 1

添加可復制的示例

 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) 

我認為使用barplot進行此操作可能會更容易,因為spineplot不會返回任何有用的信息。

默認值是以下值,但您可以將條形的寬度調整為其他變量(可以看到返回了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

在此處輸入圖片說明

經過最后的修改,我們得到了

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)

在此處輸入圖片說明

相比於

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

在此處輸入圖片說明

@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])

在此處輸入圖片說明

希望它能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM