图r两个分类变量

图r两个分类变量,r,plot,R,Plot,我使用下面的命令在R中绘制两个分类变量 性别有2个层次,收入有9个层次 spineplot(main$Gender,main$Income, xlab="Gender", ylab="Income levels: 1 is lowest",xaxlabels=c("Male","Female")) 它生成如下图表 如何用彩色绘制此图表 如何显示每个框中每个收入水平的百分比?例如,女性收入水平1占数据的21%。如何在深色区域内显示21% ################更新1 添加可复制的示例

我使用下面的命令在R中绘制两个分类变量

性别有2个层次,收入有9个层次

spineplot(main$Gender,main$Income, xlab="Gender", ylab="Income levels: 1 is lowest",xaxlabels=c("Male","Female"))
它生成如下图表

  • 如何用彩色绘制此图表
  • 如何显示每个框中每个收入水平的百分比?例如,女性收入水平1占数据的21%。如何在深色区域内显示
    21%
  • ################更新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)
    

    fail我认为使用
    barplot
    可能更容易做到这一点,因为
    spineplot
    不会返回任何有用的信息

    默认设置如下,但您可以将条形的宽度调整为其他变量(您可以看到返回的x轴坐标):

    经过最后的润色

    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)
    

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

    失败而不是彩虹,是否可能得到绿色或蓝色的阴影?@user2543622是的,尽管这取决于你所说的阴影是什么意思。例如,您可以选择一种颜色并应用不同数量的透明度
    col=Vectorize(adjustcolor)('green4',alpha.f=seq(.3,1,length.out=nrow(prp))
    或两种或两种以上完全透明的颜色,它们之间有阴影
    col=colorrmppalete(c('limegreen','darkgreen'))(nrow(prp))
    是否可以就print.perc功能编写一些说明?这将使我能够将您的解决方案应用到我的数据中。此外,是否可以显示如何以单色阴影绘制?是否可以以粗体显示百分比?此外,请让我知道如何在<代码>文本<代码>中查找颜色关键字,如浅蓝色深蓝色添加
    font=2
    。对于颜色关键字,在命令提示下键入
    colors()
    spineplot(fail, gender, col = rainbow(nlevels(gender)))
    
    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])