DEV Community

Bhu14
Bhu14

Posted on

Creating graphs in R

R is very important to analysts because it has a wide range of techniques for graphically interpreting data. The main styles are: dot plot, density plot (can be classified as histograms and kernel), line graphs, bar graphs (stacked, grouped and simple), pie charts (3D,simple and expounded), line graphs(3D,simple and expounded), box-plots(simple, notched and violin plots), bag-plots and scatter-plots (simple with fit lines, scatter-plot matrices, high-density plots and 3-D plots). The foundational function for creating graphs: plot(). This includes how to build a graph, from adding lines and points to attaching a legend.

The plot() Function:

The plot () function forms many foundations for R's basic graphics operations and serves as a means for creating many different types of graphics. plot () is a placeholder for a generic function or function family. The actual function that is called depends on the class of the object that is called.

The basic syntax for plot() function is
*plot(v, type, col, xlab, ylab) *

  • 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 for the chart.

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

Examples of plot function :

1.

# Define the cars vector with 5 values 
cars <- c(1, 3, 6, 4, 9)
# Graph the cars vector with all defaults 
plot(cars)
The default argument of type is points 
Enter fullscreen mode Exit fullscreen mode

Image description

2.

# Define the cars vector with 5 values 
cars <- c(1, 3, 6, 4, 9)
# Graph cars using blue points with lines 
plot(cars, type="o", col="blue")
# Create a title with a red, bold/italic font 
title(main="Autos",col.main="red", font.main=4)
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)