Tutorial: Using plot() function

April 19, 2012

(This article was first published on ALSTAT R Blog, and kindly contributed to R-bloggers)

Hello Readers! This is my first post as a member ofR-bloggers. In this post I’m going to talk about the basic plotting in R, fortwo dimensional. This is a tutorial for beginners in R.

To begin with, let’s define a vector first. Say wehave vector x, which is a sequence from 1 to 10 with length of 100 points. Thatis, 

Then, here’s the simplest way on plotting a yfunction using points on vector x.

Now, you can modify your plot. Say you want to add atitle on your plot. Then you can just add main=”Plot of sin(x) + cos(x)”, on the code. That is,

Adding label on both axes is achieved using xlab andylab options. Thus, we have,

Moreover, you can also change the style of yourplot, by changing the type of it. And here are the possible types of plot:

“p” for points,
“l” for lines,
“b” for both,
“c” for the lines part alone of”b”,
“o” for both overplotted,
“h” for histogram like (or high-density)vertical lines,
“s” for stair steps,
“S” for other steps,
“n” for no plotting.
So let’s say we choose type “h”. Then we have

Looking at the plot now, it seems that the lines arevery thin. To make it thicker we need to add an option lwd, which is for the linewidth. And thus, changing the line width and making it thicker, we can give avalue say 2.5.

Now, let’s change the color of it to “red”. Just apply coloption,  

But in the case of type= “p”, you can also change the size of thecircle by using cex option.

In addition, you can also change the plotting’character’, by using the pch option in R.

To leave a comment for the author, please follow the link and comment on their blog: ALSTAT R Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)