Tuesday, 4 August 2015

R graphics

R comes with rich built-in graphic functions. In this tutorial series, you are going to learn, basics to start working with R graphics.  To run all the examples in this tutorial, you have to install ggplot2 package.

R : Graphics : scatter plot
A plot is a graphical technique for representing a data set, usually as a graph showing the relationship between two or more variables.

Create a scatter plot
Scatter plot is a type of display using variables for a set of data. The data is displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis

You can create scatter plot using plot() function.

Open R console and type following commands.
> x <- runif(1000, 1, 10000)
> y <- runif(1000, 1, 10000)
> plot(x, y)

Above statements create following scattered graph.


“plot” is a function for plotting R objects.

Usage
plot(x, y, ...)

x : X coordinates of points in the plot.
y : Y coordinates of points in the plot
... : Arguments to be passed to methods

Many methods accept following arguments.

type
         What type of plot should be drawn. Possible types are

         "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.


main : an overall title for the plot.

sub : a sub title for the plot.

xlab : a title for the x axis.

ylab : a title for the y axis.

asp : the y/x aspect ratio.


Now play around with plot function, by using arguments.
> plot(x, y, main="My First Scattered Plot", sub="Scattered plot 1.1", xlab="X label", ylab="Y label")

Above statement, draws a plot and add title, sub title, x-axis label and y-axis label to the plot.


“ggplot2” package also provides qplot method to draw plots. In R console, run following statements.
> x <- runif(1000, 1, 10000)
> y <- runif(1000, 1, 10000)
> library(ggplot2)
> qplot(x,y)


“qplot” draws a graph like following.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment