Tuesday, 25 August 2015

R - Basic graphical parameters


Yo,

Progressing ahead with visualization in R. Here is another blog in the series. We are going to explore the various options available with which we can control the presentation of our graphs. Coz, presentation matters!

*wink*

Alrigh, kickstart. 

For this one I will consider the "iris" data set. Lets have a look at the first few rows.


Hmm interesting. Lets see what are the species here -


I will create a subset of iris where species = virginica


Now,

I want to see that for virginica, how does the petal length vary with sepal length. Lets have a simple plot for that

> attach(iris1)
> plot(Sepal.Length,Petal.Length)
> title("Flower Science WOW!")



Pretty bland. But I know someone who can transform this.

Now, let me introduce you to my friend par() function


So, my friend par is good at manipulating the graphical settings of the presentation. It can manipulate Both Graph as well as  Text  of the visualization.

Lets see what are the various weapons this Neo is carrying. The next few sections will specify the various options that can be used with par(). After that we will see the example.

For manipulating the symbols and lines -


  • pch - used to control the symbols on the plot ( 1 to 25)
  • cex (Sex in R :P) and it actually controls the size :P of the symbol
  • lty - the line type - 6 options
  • ltw - the line width


pch options in par() function
lty options in par() function

For manipulating colors -

  • col - for specifying the default plotting color. We can pass a vector also to this, ex, col=c("red","blue","green")
  • col.axis - color for the axis text
  • col.lab - color for the axis label
  • col.main - color of the main title
  • col.sub - color of the subtitle
  • fg - color of the foreground
  • bg - color of the background
We can specify the colors through names, hexadecimal, rgb, hsv

Names - col="red"
Hex - col="#FFFFFF"
rgb - col=rgb(1,3,1)
hsv - col=hsv(0,0,3)

For manipulating the size of the text -

  • cex - You know what it does, it controls the size
  • cex.axis - controls the size of axis text
  • cex.lab - controls the size of the axis label
  • cex.main - controls the size of the title
  • cex.sub - controls the size of the subtitle

For manipulating the Font of the text -

  • Font - The bold and italics. It accepts integer values. 1= plain, 2=bold, 3=italic, 4= Bold italic, 5=Symbols
  • font.main - the font of the main title. Accepts number
  • font.axis - the font of the axis text. Accepts number
  • font.lab - the font of the axis label. Accepts number
  • font.sub - the font of the subtitle. Accepts number family - the font family. Text input in quotes for the family of the font. eg family = "serif"
We can also control the size and margins of the graph through par() -

  • pin - dimensions in inches, its c(width,height)
  • mai - margin in inches, its c(bottom, left, top, right)
  • mar - margin in lines,  its c(bottom, left, top, right)

Alright, so we now know that par can manipulate the plots, the colors, the texts that includes your font and size (cex.....ok its no longer funny ) and the dimension of the plot area. Yay!

Few more things. most of the properties that I have mentioned for the par() can be applied within the various graph functions as well, ex plot(x,y,pch=4), but when mentioned in the par() function, these will be applied to all the graph commands that fall below a particular par() command. You can modify the properties individually for each graph command as well, even when they are below a par() function. Also, you can use multiple par() calls through out your code to apply different attributes to different set of graphs.

If you simply type par() on the console, it will return all the present graphical settings. 

Alright, I will now put a code below so that it covers few properties from each kind of manipulation, hmmm, So I want filled circles on the plot and not hollow ones, They should be filled with blue color, also I want a dashed red line. I want all the texts to be 25% larger then they are right now. I want everything in bold and I want the axis labels to be green.

>attach(iris1)
> par(col="red",cex=1.25,pch=19,font=2,col.lab="darkgreen")
> plot(Sepal.Length,Petal.Length)

Here you can see that since i have mentioned col="red" in the par function. The plot is also red. But, I want blue. I will add col attribute in the plot function -

>attach(iris1)
> par(col="red",cex=1.25,pch=19,font=2,col.lab="darkgreen")
> plot(Sepal.Length,Petal.Length)
> plot(Sepal.Length,Petal.Length,col="blue")



Next, I will add the line and Title

>attach(iris1)
> par(col="red",cex=1.25,pch=19,font=2,col.lab="darkgreen")
> plot(Sepal.Length,Petal.Length)
> plot(Sepal.Length,Petal.Length,col="blue")
>abline(lm(Petal.Length~Sepal.Length),lty=5)
> title("Colored Flower Science WOW! and cex :P")
>detach(iris1)



Here again you can see that since we did not explicitly mention the color of line, by default it picked up the red color.

Alright, with that another blog is published. Phew!

If you want to understand about the other functions I have used in my code check out my previous post - plotting a simple graph in R

And before you leave, do leave a comment and share this post. Why do you think I am doing this? I love you :)

Till then take care and practice cex!








No comments:

Post a Comment