2016-07-11 6 views
1

私は水平バープロットを作成して2つのテーブルを比較したいと思います。私はすでに比較を行い、比率のテーブルを作成しました。私は計算された比率に基づいて、1を置く中央に比較のための水平バープロット2つのデータベースの比率

Example

> dput(data) 
structure(list(Name=c('Mazda RX4','Mazda RX4 Wag','Datsun 710','Hornet 4 Drive', 
'Hornet Sportabout','Valiant','Duster 360','Merc 240D','Merc 230','Merc 280','Merc 280C', 
'Merc 450SE','Merc 450SL','Merc 450SLC','Cadillac Fleetwood','Lincoln Continental', 
'Chrysler Imperial','Fiat 128','Honda Civic','Toyota Corolla'),ratio=c(1.393319198903125, 
0.374762569687951,0.258112791829808,0.250298480396529,1.272180366473129,0.318000456484454, 
0.264074483447591,0.350798965144559,2.310541690719624,1.314300844213157,1.18061486696761, 
0.281581177092538,0.270164442687919,2.335578882236703,2.362339701969396,1.307731925943769, 
0.347550384302281,0.232276047899868,0.125643566969327,0.281209747680576),Freq=c(2L,9L,2L,2L, 
4L,2L,2L,3L,3L,5L,2L,2L,2L,7L,2L,4L,4L,2L,2L,4L)),.Names=c('Name','ratio','Freq'),class= 
'data.frame',row.names=c(NA,20L)) 

私はそのような何かを達成したいと思います:データがどのように見えるかだ

例えば右に3、左に0になる適切なスケールを入れたいと思っています(もちろん違うかもしれません)。

それぞれの車には別々のバーがあります。このプロットには20の棒が表示されます。

さらに、列Freqの数値をプロットに入力します。それは義務ではありませんが、助けになります。

+2

バーでしょうゼロから1.4〜にして、別のと色を3にする? –

答えて

3

plot

## plot precomputations 
yexpand <- 0.2; 
barheight <- 0.8; 
xlim <- c(0,3); 
xticks <- seq(xlim[1L],xlim[2L],0.25); 
ylim <- c(1-barheight/2-yexpand,nrow(data)+barheight/2+yexpand); 
yticks <- seq_len(nrow(data)); 
cols <- c('#6F7EB3','#D05B5B'); 

## draw plot 
par(mar=c(5,4,4,2)+0.1+c(0,3,0,0)); 
plot(NA,xlim=xlim,ylim=ylim,xaxs='i',yaxs='i',axes=F,ann=F); 
segments(xlim[1L],ylim[1L],xlim[1L],ylim[2L],xpd=NA); 
axis(1L,xticks,cex.axis=0.7); 
axis(2L,yticks,data$Name,las=2L,cex.axis=0.7); 
mtext(expression(italic(Ratio)),1L,3); 
mtext(expression(italic(Car)),2L,5.5); 
mtext(data$Freq,4L,0.75,at=yticks,las=2L,cex=0.7); 
y1 <- seq_len(nrow(data))-barheight/2; 
y2 <- seq_len(nrow(data))+barheight/2; 
rect(xlim[1L],y1,data$ratio,y2,col=cols[1L],lwd=0.5); 
rect(data$ratio,y1,xlim[2L],y2,col=cols[2L],lwd=0.5); 
abline(v=1); 
4

私は本当に1まで追加なし数量(または共通合計)がないように、そのプロットは、あなたのデータとあまり理にかなっているか表示されません。それは比ではなく、比率で意味をなさげることができます。私は何かを見逃しているかもしれない...おそらくあなたはこのようなものを探しているのだろうか?

library(ggplot2) 

r <- range(d$ratio) 
br <- seq(floor(r[1]), ceiling(r[2]), 0.5) 

ggplot(d, aes(x = Name, y = ratio - 1)) + 
    geom_bar(stat = 'identity', position = 'identity') + 
    coord_flip() + 
    ylab('ratio') + xlab('car') + 
    scale_y_continuous(breaks = br - 1, labels = br) + 
    theme_bw() 

enter image description here

右側にラベルのgeom_text(aes(label = Freq), y = r[2] - 0.95)を追加します。

それとも、1(少しトリッキー)の値を中心にしたい場合:マツダRX4のために例えばので

r <- range(d$ratio) 
m <- ceiling(max(abs(range(d$ratio)))) 
br <- seq(-m + 1, m - 1, 0.25) 

ggplot(d, aes(x = Name, y = ratio - 1)) + 
    geom_bar(stat = 'identity', position = 'identity') + 
    geom_text(aes(label = Freq), y = m - 1.1) + 
    coord_flip() + 
    ylab('ratio') + xlab('car') + 
    scale_y_continuous(breaks = br, labels = br + 1, limits = c(-m + 1, m - 1), 
        expand = c(0, 0)) + 
    theme_bw() 

enter image description here

+0

それは私にとっても役に立つかもしれません。あなたは、 "1"を中央に置き、0.25ブレークを行うようにプロットを強制する方法を知っていますか?私は自分で試しましたが、成功しませんでした。 –

+2

これはややこしいですが、編集を参照してください。 – Axeman

関連する問題