2017-06-25 20 views
2

私は以下のコマンドを使用して2つのカテゴリ変数をプロットします。plot r 2つのカテゴリ変数

性別は2レベル、所得は9レベルです。

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

それはenter image description here

  1. どのように私は色でこのチャートをプロットすることができます以下のようなグラフを作成しますか?
  2. 各ボックス内の所得レベルの%をどのように表示できますか?例えば、女性所得レベル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) 

答えて

2

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

enter image description here

それはあなたを助けることができると思います。

+0

あなたのprint.perc関数についての説明はほとんど書きませんか?それは私のデータにあなたのソリューションを適用することが可能になります – user2543622

+0

あまりにも多くの詳細を書いてくださいしないでください...ちょうどいくつかのキーポイント – user2543622

+0

も、単一の色の陰にプロットする方法を示すことは可能でしょうか?とにかく太字でパーセントを取得する – user2543622

3

が、私はbarplotspineplot以来でこれを行うことが容易かもしれないと思う再現可能な例を追加有用なものは何も返されません。

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 

enter image description here

付:

デフォルトは次のようになりますが、あなたは、いくつかの他の変数にバーの幅を調整することができます(座標が返され、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) 

enter image description here

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

enter image description here

+0

虹の代わりに、緑または青の色合いを得ることは可能でしょうか? – user2543622

+0

@ user2543622はい、それはあなたが陰影の意味に依存しますが。例えば、1つの色をとり、異なる量の透明度を適用することができます。col = Vectorize(adjustcolor)( 'green4'、alpha.f = seq(.3,1、length.out = nrow(prp))) 'または2つcolor = colorRampPalette(c( 'limegreen'、 'darkgreen'))(nrow(prp)) ' – rawr

関連する問題