r - Horizontal barplot for comparison two data - based on ratio -


i create horizontal barplot compare 2 of tables. did comparison , created table ratio.

that's how data looks like:

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

i achieve that:

example

in middle put 1. based on calculated ratio put proper scale goes 3 right example , 0 left (can different of course).

each of cars should have separate bar. give 20 bars on plot.

additional thing put numbers column freq on plots. it's not obligatory help.

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

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -