Ofter we suffer from a common problem while making graphs in R. Often we think of customized axes and labels in R plot, may be even inserting text. This is an effort to aggregate some of the things we look for every now and then.
Here some random numbers were generated to make a plot. First let us see what we get as default.
set.seed(111)
x=round(runif(50, 0, 30))
set.seed(121)
y=10+(0.5*x+rnorm(50,0,10))^2
plot(y~x, main="Default Plot")
Now waht we see here is the default plot generated by R. The symbols are by default chosen as circles, color is by default black, and the axes ranges and labels are chosen by default. We see the axis tick marks are all parallel to the respective axes. Often we want to make it custimized like,
Let us try to do these customizations. We will only work on the base R graphics.
This is very simple task and done by passingpch
and col
arguments.
plot(y~x, pch=16, main="Solid Circle")
plot(y~x, pch=4, main="X Mark")
plot(y~x, pch=5, main="Diamond")
plot(y~x, pch=16, col="red", main="Solid Red Circle")
plot(y~x, pch=16, cex=2, main="Change Symbol Size")
plot(y~x, xaxt="none", main="Turn off x-axis")
plot(y~x, yaxt="none", main="Turn off y-axis")
plot(y~x, xaxt="none", main="Redefined/Customized x-axis")
axis(1, seq(0,30,10))
plot(y~x, yaxt="none", main="Redefined/Customized y-axis")
axis(2, seq(0,700,50))
plot(y~x, yaxt="none", main="Rotate y-axis tick labels")
axis(2, seq(0,700,100),las=2)
plot(y~x, xaxt="none", main="Bold x-axis tick labels")
axis(1, seq(0,30,5), font=2)
plot(y~x, yaxt="none", main="Bold y-axis tick labels")
axis(2, seq(0,700,100),las=2, font=2)
plot(y~x, yaxt="none", ylim=c(0,1000), main="Extended Y limit")
axis(2, seq(0,1000,100),las=2, font=2)
plot(y~x, yaxt="none", xlim=c(0,40), main="Extended X limit")
axis(2, seq(0,700,100),las=2, font=2)
plot(y~x, xlab="New X label", ylab="New Y label",main="Change axis lables")
plot(y~x)
mtext(side=3, line=0.2, "Reduced Space here")
plot(y~x)
mtext(side=3, line=0.2, "Reduced Space here, bold",font=2)
plot(y~x)
mtext(side=3, "Reduced font size, bold",font=2,cex=0.5)
plot(y~x)
mtext(side=3, "Bigger font size, bold",font=2,cex=2)
plot(y~x,xaxt="none")
axis(1, seq(0,30,1))
mtext(side=3, "X Axis tick labels are messy",font=2)
plot(y~x,xaxt="none")
axis(1, seq(0,30,1),las=2)
mtext(side=3, "Rotate X-axis tick labels",font=2)
plot(y~x,xaxt="none")
axis(1, seq(0,30,1),las=2, cex.axis=0.8, font=2)
mtext(side=3, "Reduce X-axis tick labels font size",font=2)
plot(y~x,xaxt="none")
axis(1, seq(0,30,1),las=2, cex.axis=0.8, font=2,col.axis="red")
mtext(side=3, "Change axis color",font=2)
par(mar=c(7,6,5,1)) ## set the margin
## starting from bottom, goes clockwise
plot(y~x, xaxt="none", yaxt="none", xlab="",ylab="",
col="deeppink",cex=1.5,pch=16)
axis(1, seq(0,30, 1),las=2, font=2,cex.axis=0.8)
axis(2, seq(0,700,100),las=2, font=2)
## draw some lines
abline(h=seq(0,700,100), v=seq(0,30, 1), lty=3, col="gray")
mtext(side=1, line=2, "X-axis label, bold, bigger", col="blue", font=2,cex=1.2)
mtext(side=2, line=3, "Y-axis label, bold, bigger", col="orange", font=2, cex=1.2)
mtext(side=3, line=0.5, "Main title, italic", col="forestgreen", font=3, cex=2)
text(x=5, y=600, "Add some text, reduced font",col="gold4", font=2, cex=0.8)