2016-07-11 35 views
1

我想創建一個水平barplot來比較我的兩個表格。我已經做了比較,並創建了一個比例表。用於比較的水平barplot兩個數據 - 基於比率

這就是數據的樣子:

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

我想實現這樣的事情:

Example

在中間我會把1.基於計算的比我想要將合適的比例增加到3,例如右邊0(當然可以不同)。

每輛車應該有一個單獨的酒吧。它會給這個陰謀20個酒吧。

另外一件事情是將Freq列上的數字放在圖上。這不是強制性的,但會有所幫助。

+2

因此,例如馬自達RX4酒吧會從零到約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的值(有點比較麻煩):

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