Learning R in Practice

title: "R plot base system" author: "Jiayi (Jason) Liu" date: "January 26, 2015"

output: html_document

Base system

The base plotting servers as the foundation of plotting in R.

General instruction

graph style

The following attributes can be used in most base ploting functions.

  • pch ploting symbol
  • lty line style
  • lwd line width
  • col color

Here are some attributes controling the axes layout.

  • xlab, ylab
  • xlim, ylim

Histogram

  • hist takes a numeric list and calculate the histogram. The most common tuning parameters are break (control breaking points) and freq (return counts or probability). It also returns histogram class with values of mids middle points, counts and density.

    data <- sample(1:40, 400, replace=TRUE)
    x <- hist(data, breaks=10, freq=FALSE)
    lines(x$mids, x$density, type='h', lwd=3, col=4)
    

The last command lines is a general plotting function. Here we use it with type of hstogram, line width lwd=3 and color col=4.

  • density converts the data to a smooth distribution with specified kernel. Notice that the boundary effect might result problematic representation of the data distribution at discontinued places.

    h <- density(data,n=10,from=0,to=40)
    lines(h$x, h$y, type='l')
    

Basic histogram 1

  • table takes variables as individual levels and counts for each. With barplot we can also create the histogram.
    barplot(table(data))
    
    Notice, for barplot, it use the names of the data as the x tick labels.

Scatter plot