Jul 15, 2014

How to Plot Multiple Parallel Graphs in [R]

The Code:

par(mfrow=c(2,2)) % the matrix indicating the number of graphs and position.
plot(Rev, type="o", main="NHI: Revenues", xaxt='n', xlab='') % "Rev" is the variable you want to plot; type = "o" is for a line graph; main is for your title; xaxt and xlab are for  eliminating default axis labels and values.
axis(1, at=1:17, labels=Year, las = 2, cex.lab=1, cex.axis=.5, cex.main=1.5, cex.sub=1.5)
grid() % axis is to customize your axis; at is for 17 values, in this case the number of years; labels is to include your own axis values, in this case the variable "Year", las is for the position of the values, the others are for sizes of axis values.
plot(Exp, type="o", main="NHI: Expenditures", xaxt='n', xlab='')
axis(1, at=1:17, labels=Year, las = 2, cex.lab=1, cex.axis=.5, cex.main=1.5, cex.sub=1.5)
grid()
plot(Loans, type="o", main="NHI: Loans", xaxt='n', xlab='')
axis(1, at=1:17, labels=Year, las = 2, cex.lab=1, cex.axis=.5, cex.main=1.5, cex.sub=1.5)
grid()
plot(d.s, type="o", main="NHI: d/s", xaxt='n', xlab='')
axis(1, at=1:17, labels=Year, las = 2, cex.lab=1, cex.axis=.5, cex.main=1.5, cex.sub=1.5)
grid()

It took me some time to figure it out because I could not install the ggplot2 plotting system. 


If you find [R] useful, specially for plots, I hope this helps.  By the way, that is data from the Taiwan National Health-Insurance (NHI) system. "d/s" stands for deficit/surplus

No comments:

Post a Comment