Friday, 28 August 2015

Plotting multiple graphs in R with fine control


Wassup,

This one is my fourth post in the R graphical series, post this I will give graphs some pause. We have seen how to plot simple graphs in R, how to plot multiple graphs together and how to plot multiple graphs with more control. Now, we are going get even more sci fi. We are going to see how to superimpose one graph over the other. And this time again we gonna call our old friend, yep you got it the par() function.



By now we know that par() has many super powers. In this post I will reveal to you another one – “fig”

The syntax of fig argument is as follows –

fig=c(x1,x2,y1,y2)

Where x1 and x2 are start and end points on the x axis from 0 to 1. And y1 and y2 are the start and end points on the y axis from 0 to 1. These dimensions will include the entire graph area including the labels. It will get clear with the examples.

One more thing after the first plot with each plot we need to put new argument as True in the par() function


Alright. * popping fingers *

>par(fig=c(0,0.8,0,0.8))
>plot(mtcars$wt,mtcars$wt)
>par(fig=c(0,0.8,0.62,1),new=T)
>hist(mtcars$wt)
>par(fig=c(0.6,1,0,1),new=T)
>boxplot(mtcars$mpg,axes=FALSE)

In the above code our first graph is a simple plot with x1=0,x2=0.8,y1=0,y2=0.8 . Things get interesting with second plot. Here I have come with the values of fig after trial and error and most of the times you will be required to do the same as well. Also notice that I have used the new=T argument.
If you increase the size of the y dimension for the second plot by lowering down y1, you will notice that the graphs will start overlapping.
For the final graph again I have come up with dimensions after trial and error and kept new argument as T.
And massaging enter will give. Boom –

 I will create one summary post on all the posts on graphs I have created so far. I would sincerely appreciate if you can let me know your thoughts on my work.

For your convenience I have added a linkedin Share button. You will feel amazing after using it. Trust me :)

You can check out my other posts on Graphs through R below -

Plotting a simple graph in R

R - Basic graphical parameters


See you next time.

Till then

Stay Awesome


No comments:

Post a Comment