Monday, February 9, 2015

R: graphics by plot() (I)


R: graphics by plot() (I)

Abstract: How to create graphs using the base function plot() in R


The simple usage
Here, I listed three equal patterns for the usage of plot(). The three patterns share the identical graph.
> head(state.x77)
Population Income Illiteracy Life Exp Murder HS Grad Frost Area
Alabama 3615 3624 2.1 69.05 15.1 41.3 20 50708
Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432
Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417
Arkansas 2110 3378 1.9 70.66 10.1 39.9 65 51945
California 21198 5114 1.1 71.71 10.3 62.6 20 156361
Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766
#plotting
> plot(x=state.x77[,'Population'], y=state.x77[,'Income'])
> plot(state.x77[,'Income']~state.x77[,'Population'])
> plot(Income~Population, state.x77)



After that, a more decorated graph is produced. Though the same data, the x-axis, y-axis, title, points,and additional lines are so different.
plot(Income~Population, data=state.x77, pch=ifelse(Illiteracy>1.5, 20, 2), cex=1, col=ifelse(Illiteracy>1.5, 'red', 'blue'), xlab='Population of US states', ylab='Incomes on average', main='Incomes vs. Populations')
abline(v=5000, lwd=2, lty=2)


The arguments pch=, col=, and bg= specify symbols, filled and border colors when plotting points.
> plot(x=1:25, y=rep(0, length=25), xlab='pch=', pch=1:25);text(x=1:25,y=-0.2, 1:25)
>
colors<-pallete()
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" "gray"
> barplot(1:8,col=colors)


> plot(x=1:6, y=1:6, type='n');abline(v=1:6, lty=1:6, lwd=10)



writing date: 2014.03.10, 2015.02.09


No comments:

Post a Comment