August, 2017

Plotting points

plot

mtcars data analysis:

+ mtcars contains (mpg: mile per gallon) and (disp: displacement) ...
    attach(mtcars)
    head(mtcars, n = 2)
##               mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
## Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
  • ?mtcars

plot

plot()

  • the relation between two variables can be explored by plot.
plot(mpg ~ disp, data = mtcars)

plot

plot(mpg ~ disp, data = mtcars)
  • mtcars is datafame
  • mpg~disp denotes the relation between mpg and disp which are the column name of the dataframe.
  • In mpg~disp a points take the value of y-axis from mpg and of x-axis from disp.

plot

Exercise

  • Draw a plot of which y-axis is hp (horse power) and x-axis is disp.

plot

plot(hp ~ disp, data = mtcars)

plot

Draw a plot by vectors.

  • We can use vectors to draw a plot.
x = rnorm(100)
y = 2+2*x + rnorm(100)
plot(x,y, main = "plot (x-y)")
  • rnorm(100) choose 100 random number for the standard normal distribution.
  • The value of point on the y-axis is given by \(y = 2 + 2x + \epsilon\) where \(\epsilon \sim N(0, \sigma^2\).
  • main = "plot (x-y)" adds the title in the plot.

plot

plot

Drawing option

  • plot function can draw some graphical object between points which are adjacent in vectors (not values).

option: the type of the graphical object

  • type = 'p': point
  • type = 'l': line
  • type = 'b' : both point and line
  • type = 's' : step

  • By ?plot you can see more information about options.

plot

Drawing option

x = seq(-2,2, length = 10)
y = x^2
plot(x,y, type = 'p', main = "y = x^2")

plot

Drawing option

  • What is the graph with type = 'b'?

plot

Drawing option

  • By lty option we can set a type of lines connecting the points
  • ?par and chekc the lty option.
plot(x,y, type = "b", lty = 3, main = "y = x^2")

plot

Drawing option

  • pch (plotting symbol)
plot(x,y, type = "b", lty = 3, pch = 2, main = "y = x^2")

plot

Drawing option

  • Exercise: explain the result of the following code.
plot(x = 1:25,y = rep(0, 25), pch = 1:25)

plot

Drawing option

  • col (color)
plot(x,y, type = "b", lty = 3, pch = 2, 
     col = "blue", main = "y = x^2")

plot

Drawing option

  • The function colors() returns the list of the names of available colors
colors()[1:10]
##  [1] "white"         "aliceblue"     "antiquewhite"  "antiquewhite1"
##  [5] "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine"   
##  [9] "aquamarine1"   "aquamarine2"

plot

Axis option

  • label on axis: xlab, ylab
plot(x,y, type = "b", xlab = "xx", ylab = "yy", main = "y = x^2")

plot

Drawing multiple plots

  • Using Formula provides a plot with multiple panels.
plot(~mpg+disp+drat,data=mtcars, 
   main="Simple Scatterplot Matrix")

Add lines and points

abline

  • abline is a function to draw on the existing plot.

abline() draws

  • the line with intercept \(a\) and slope \(b\)
  • the horizontal line
  • the vertical line
x = rnorm(100)
y = 2+2*x + rnorm(100)
plot(x,y, pch = 20, main = 'scatter plot')
abline(a = 1, b = 2, col = "red")
abline(v = 1, col = "blue")
abline(h = 1, col = "green")

abline

points

options

  • type is same as the option in the plot().
  • lty is same as the option in the plot()
plot(x = 1,y = 1, type = 'n', xlim = c(0,10), ylim = c(0,5),
     xlab = 'time', ylab = '# of visiting')
x = 0:10
set.seed(1)
y = rpois(length(x), lambda = 1)
points(x,y, col = "blue", type = "s")
points(x,y, col = "red", type = "l")

plot

range of axis

  • options:xlim and ylim. Note that the option requires the vector with length 2.
x = 0:10
set.seed(1)
y = rpois(length(x), lambda = 1)
plot(x,y, type = "b", xlab = "x-variable", 
     ylab = "y-variable", main = "y = x^2",
     xlim = c(-1,1), ylim = c(-1,1))

plot

exercise

plot(mpg~disp, data = cars, xlab = "displacement", ylab = "mile/gallon",
     main = "scatter plot", pch = 20, col = 'darkblue')

points

exercise

lines

  • lines() draws line on existing graph.
  • The function connect the points in the vector by order.
plot(0,0, type = 'n', xlim = c(-2,2), ylim = c(-2,2))
x = c(-2, 1, 0, 1, 0)
y = c(0, -1, 2, -2, 1)
lines(x,y)

lines

lines

  • The value NA is not connected with any points so that we can disconnect the line by using NA.
plot(0,0, type = 'n', xlim = c(-2,2), ylim = c(-2,2))
x = c(-2, 1, NA, 1, 0)
y = c(0, -1, NA, -2, 1)
lines(x,y)

lines

lines

options

  • lty: line type
plot(0,0, type = 'n', xlim = c(-2,2), ylim = c(-2,2))
x = c(-2, 1, NA, 1, 0)
y = c(0, -1, NA, -2, 1)
lines(x,y, lty = 2)

lines

plot(0,0, type = 'n', xlim = c(-2,2), ylim = c(-2,2))
x = c(-2, 1, NA, 1, 0)
y = c(0, -1, NA, -2, 1)
lines(x,y, lty = 2)

lines

exercise

  • Explain the following code.
plot(0,0, type = 'n', xlim = c(1,5), ylim = c(0,2))
x = seq(1,5, by = 1)
abline(v = x, lty = 1:length(x))

legend

  • legend() adds a legend on exsiting plot. ### arguments
  • x: position on x, y: position on y (these can be replaced by shortcuts such as "bottomleft" )
  • legedn: generally character vector
  • color, pch, lty…
z = sort(rnorm(100))
y1 = 2+ x + rnorm(100)
plot(z, y1, col = "blue", pch = 3)
points(z, y1/2, col = "red", pch = 19)
legend("topright", c("pch_3", "pch_19"), col = c("blue", "red"),
       pch = c(3,19))

legend

legend

Exercise

  • Add the legend on the 'bottomright'

Graphic parameter: par()

par()

  • par() set numerous graphical parameters in R. The function controls the layout of figure, the title, symbol of point and so on.

frequently used parameter options

  • par(mfrow = c(2,1)): \(2\times 1\) layout
  • par(cex = 1.2): set the character expansion be 1.2 (1.2 times larger)
  • par(bg = 'gray90'): set the color of backgroupd by gray90
  • par(las = 2): set the text on axis be orthogoal to the axis.
  • par( mai = c(1,2,3,4)): from the bottom clockwisely, set the margin be 1,2,3,and 4.

Se the document by ?par

par()

par (mfrow = c(2,2), bg = 'gray50', col = 'white',
     col.main = 'lightblue', col.axis = 'yellow', 
     col.lab = 'lightgreen')
x = rnorm(100)
y = 2+2*x + rnorm(100)
plot(x,y, main = "plot (x-y)-1", pch = 20)
y = 2+x + rnorm(100)
plot(x,y/2, main = "plot (x-y)-2")
y = 2+x + rnorm(100)
plot(x,y/3, main = "plot (x-y)-3", col.main = 'black')
y = 2+x + rnorm(100)
plot(x,y/4, main = "plot (x-y)-4", col.axis = 'white')

par()