August, 2017
+ 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(mpg ~ disp, data = mtcars)
plot(mpg ~ disp, data = mtcars)
mpg~disp
denotes the relation between mpg
and disp
which are the column name of the dataframe.mpg~disp
a points take the value of y-axis from mpg
and of x-axis from disp
.hp
(horse power) and x-axis is disp
.plot(hp ~ disp, data = mtcars)
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.main = "plot (x-y)"
adds the title in the plot.type = 'p'
: pointtype = 'l'
: linetype = 'b'
: both point and linetype = 's'
: step
By ?plot
you can see more information about options.
x = seq(-2,2, length = 10) y = x^2 plot(x,y, type = 'p', main = "y = x^2")
type = 'b'
?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(x,y, type = "b", lty = 3, pch = 2, main = "y = x^2")
plot(x = 1:25,y = rep(0, 25), pch = 1:25)
plot(x,y, type = "b", lty = 3, pch = 2, col = "blue", main = "y = x^2")
colors()
returns the list of the names of available colorscolors()[1:10]
## [1] "white" "aliceblue" "antiquewhite" "antiquewhite1" ## [5] "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine" ## [9] "aquamarine1" "aquamarine2"
xlab
, ylab
plot(x,y, type = "b", xlab = "xx", ylab = "yy", main = "y = x^2")
plot(~mpg+disp+drat,data=mtcars, main="Simple Scatterplot Matrix")
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")
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")
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(mpg~disp, data = cars, xlab = "displacement", ylab = "mile/gallon", main = "scatter plot", pch = 20, col = 'darkblue')
lines()
draws line on existing graph.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)
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)
lty
: line typeplot(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)
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)
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()
adds a legend on exsiting plot. ### argumentsz = 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))
par(mfrow = c(2,1))
: \(2\times 1\) layoutpar(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 (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')