library(ggplot2) # This package is used here only for its "diamonds" data set.
hist(x = diamonds$carat, main = "Carat Histogram", xlab = "Carat")
This shows the actual counts for each interval. For relative frequencies, use:
hist(x = diamonds$carat, main = "Carat Histogram", xlab = "Carat", freq = FALSE)
plot(x = diamonds$carat, y = diamonds$price)
The plot() function has the optional argument type. Possible types are:
Other optional arguments include:
Note that the plotted variables can be from separate data frames.
x <- c(-0.5, 0, 1, 1, 1.5); y <- c( 0, 0, 2, 0, 0)
plot(x = x, y = y, lwd = 3, frame = FALSE, type = "l")
x <- c(-0.5, 0, 1, 1, 1.5); y <- c( 0, 0, 2, 0, 0)
plot(x = x, y = y, lwd = 3, frame = FALSE, type = "l")
polygon(x = c(0, .75, .75, 0), y = c(0, 0, 1.5, 0), lwd = 3, col = "lightblue")
Or we can fill the area under the graph:
x <- c(-0.5, 0, 1, 1, 1.5); y <- c( 0, 0, 2, 0, 0)
plot(x = x, y = y, lwd = 3, type = "l")
polygon(x = c(0, 1, 1, 0), y = c(0, 0, 2, 0), lwd = 3, col = "yellow")
This is done with the curve(function(x), xmin, xmax) command. Examples:
curve(x^2, -2, 2)
curve(dnorm(x), -3, 3)
add = TRUEcurve(x^2, -2, 2, lwd = 1, lty = 1)
curve(dnorm(x), add = TRUE, lwd = 1, lty = 2)
When one variable is plotted on the horizontal (x) axis, and multiple variables are plotted on the vertical (y) axis, we can use the matplot() command:
# Define one x vector for all:
year <- c(2008,2009,2010,2011,2012,2013)
# Define a matrix of y values:
product1 <- c(0,3,6,9,7,8)
product2 <- c(1,2,3,5,9,6)
product3 <- c(2,4,4,2,3,2)
sales <- cbind(product1,product2,product3)
# plot
matplot(year,sales, type="b", lwd=c(1,2,3), col="black" )
par(mfrow=c(2,1)) # To properly stack the graphs
curve(x^2, -2, 2)
curve(dnorm(x), -3, 3)
The par(mfcol = c(rows,columns)) command could also be use to arrange multiple graphs.
Here’s a way to arrange three plots using the layout() function:
First, define a matrix:
(m <- matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
## [,1] [,2]
## [1,] 1 1
## [2,] 2 3
Then, use the matrix in the layout() function:
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
# The first chart is sprawled across the first row
# The 2nd and 3rd plots are side by side in the second row
hist(mpg$displ)
hist(mpg$cty)
hist(mpg$hwy)
x <- c(1,3,4,7,8,9)
y <- c(0,3,6,9,7,8)
plot(x,y, main="Example for an Outlier") # Just dots
points(8,1) # add a point
abline(a=0.31,b=0.97,lty=2,lwd=2) # add a line; a=intercept; b=slope
text(7,2,"outlier",pos=3) # add a label
arrows(7,2,8,1,length=0.15) # add an arrow, from (7,2) to (8,1)
curve( dnorm(x,0,1), -10, 10, lwd=1, lty=1)
curve( dnorm(x,0,2),add=TRUE, lwd=2, lty=2)
curve( dnorm(x,0,3),add=TRUE, lwd=3, lty=3)
legend("topright",c("sigma=1","sigma=2","sigma=3"), lwd=1:3, lty=1:3)
More sophisticated formatting:
curve( dnorm(x,0,1), -10, 10, lwd=1, lty=1)
curve( dnorm(x,0,2),add=TRUE, lwd=2, lty=2)
curve( dnorm(x,0,3),add=TRUE, lwd=3, lty=3)
legend("topleft",expression(sigma==1,sigma==2,sigma==3),lwd=1:3,lty=1:3)
## The expression next looks like LaTeX code without the backslashes
text(6,0.3, expression(f(x)==frac(1,sqrt(2*pi)*sigma)*e^{-frac(x^2,2*sigma^2)}))
Downloading and preparing the data:
# load data set
library(foreign)
affairs<-read.dta("http://fmwww.bc.edu/ec-p/data/wooldridge/affairs.dta")
# Generate "Factors" to attach labels
unique(affairs$kids) # 0 1; shows the unique values in the variable
## [1] 0 1
haskids <- factor(affairs$kids,labels=c("no","yes"))
unique(affairs$ratemarr) # 4 5 3 2 1; these 5 unique values can be converted to a factor
## [1] 4 5 3 2 1
mlab <- c("very unhappy","somewhat unhappy","average", "somewhat happy", "very happy")
marriage <- factor(affairs$ratemarr, labels=mlab)
pie(table(marriage), col = gray(seq(0.2,1,0.2)))
barplot(table(marriage), horiz = TRUE, las = 1, main = "Distribution of Happiness")
barplot(table(haskids, marriage), horiz = TRUE, las = 1, legend = TRUE, args.legend = c(x="bottomright"), main = "Happiness by Kids")
barplot(table(haskids, marriage), beside = TRUE, las = 2, legend = TRUE, args.legend = c(x="top"), main = "Happiness by Kids")
This is it for now.