How to create line chart and scatterplot in R Programming

 Line Chart

A line chart is a graph that connects a series of points by drawing line segments between them. These points are ordered in one of their coordinate (usually the x-coordinate) value. Line charts are usually used in identifying the trends in data.

The plot() function in R is used to create the line graph.

Syntax

The basic syntax to create a line chart in R is −

plot(v,type,col,xlab,ylab)

Following is the description of the parameters used −

·        v is a vector containing the numeric values.

·        type takes the value "p" to draw only the points, "l" to draw only the lines and "o" to draw both points and lines.

·        xlab is the label for x axis.

·        ylab is the label for y axis.

·        main is the Title of the chart.

·        col is used to give colors to both the points and lines.

Example

A simple line chart is created using the input vector and the type parameter as "O". The below script will create and save a line chart in the current R working directory.

# Create the data for the chart.

v <- c(7,12,28,3,41)

 # Plot the bar chart.

plot(v,type = "o")

 

Line Chart Title, Color and Labels

The features of the line chart can be expanded by using additional parameters. We add color to the points and lines, give a title to the chart and add labels to the axes.

Example

# Create the data for the chart.
v <- c(7,12,28,3,41)
 
# Plot the bar chart.
plot(v,type = "o", col = "red", xlab = "Month", ylab = "Rain fall",
   main = "Rain fall chart")

 

Multiple Lines in a Line Chart

More than one line can be drawn on the same chart by using the lines()function.

After the first line is plotted, the lines() function can use an additional vector as input to draw the second line in the chart,

# Create the data for the chart.
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)
 # Give the chart file a name.
png(file = "line_chart_2_lines.jpg")
 
# Plot the bar chart.
plot(v,type = "o",col = "red", xlab = "Month", ylab = "Rain fall", 
   main = "Rain fall chart")
 lines(t, type = "o", col = "blue")

 

Scatterplot

Scatterplots show many points plotted in the Cartesian plane. Each point represents the values of two variables. One variable is chosen in the horizontal axis and another in the vertical axis.

The simple scatterplot is created using the plot() function.

Syntax

The basic syntax for creating scatterplot in R is −

plot(x, y, main, xlab, ylab, xlim, ylim, axes)

Following is the description of the parameters used −

·        x is the data set whose values are the horizontal coordinates.

·        y is the data set whose values are the vertical coordinates.

·        main is the tile of the graph.

·        xlab is the label in the horizontal axis.

·        ylab is the label in the vertical axis.

·        xlim is the limits of the values of x used for plotting.

·        ylim is the limits of the values of y used for plotting.

·        axes indicates whether both axes should be drawn on the plot.

Example

We use the data set "mtcars" available in the R environment to create a basic scatterplot. Let's use the columns "wt" and "mpg" in mtcars.

input <- mtcars[,c('wt','mpg')]

print(head(input))

When we execute the above code, it produces the following result −

                                              wt      mpg

Mazda RX4                          2.620   21.0

Mazda RX4 Wag                  2.875   21.0

Datsun 710                           2.320   22.8

Hornet 4 Drive                     3.215   21.4

Hornet Sportabout                3.440   18.7

Valiant                                  3.460   18.1

 

Creating the Scatterplot

The below script will create a scatterplot graph for the relation between wt(weight) and mpg(miles per gallon).

# Get the input values.
input <- mtcars[,c('wt','mpg')]
 
# Give the chart file a name.
png(file = "scatterplot.png")
 
# Plot the chart for cars with weight between 2.5 to 5 and mileage between 15 and 30.
plot(x = input$wt,y = input$mpg,
   xlab = "Weight",
   ylab = "Milage",
   xlim = c(2.5,5),
   ylim = c(15,30),                  
   main = "Weight vs Milage"
)

Scatterplot Matrices

When we have more than two variables and we want to find the correlation between one variable versus the remaining ones we use scatterplot matrix. We use pairs() function to create matrices of scatterplots.

Syntax

The basic syntax for creating scatterplot matrices in R is −

pairs(formula, data)

Following is the description of the parameters used −

·        formula represents the series of variables used in pairs.

·        data represents the data set from which the variables will be taken.

Example

Each variable is paired up with each of the remaining variable. A scatterplot is plotted for each pair.

# Give the chart file a name.
png(file = "scatterplot_matrices.png")
 
# Plot the matrices between 4 variables giving 12 plots.
 
# One variable with 3 others and total 4 variables.
 
pairs(~wt+mpg+disp+cyl,data = mtcars,
   main = "Scatterplot Matrix")

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction