ggplot2 Version of Figures in "25 Recipes for Getting Started with R"

In order to provide an option to compare graphs produced by basic internal plot function and ggplot2, I have recreated the figures in the book, 25 Recipes for Getting Started with R, with ggplot2.

The code used to create the images is in separate paragraphs, allowing easy comparison.

1.16 Creating a Scatter Plot

plot(cars)

ggplot(cars,aes(speed,dist))+geom_point()

1.17 Creating a Bar Chart

heights <- tapply(airquality$Temp, airquality$Month, mean)
par(mfrow=c(1,2))
barplot(heights)
barplot(heights,
        main="Mean Temp. by Month",
        names.arg=c("May", "Jun", "Jul", "Aug", "Sep"),
        ylab="Temp (deg. F)")

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
require(gridExtra)
heights=ddply(airquality,.(Month), mean)
heights$Month=as.character(heights$Month)
p1 <- ggplot(heights, aes(x=Month,weight=Temp))+
    geom_bar()
p2 <- ggplot(heights, aes(x=factor(Month,
                          labels=c("May", "Jun", "Jul", "Aug", "Sep")),
                          weight=Temp))+
    geom_bar()+
    ggtitle("Mean Temp. By Month") +
    xlab("") +
    ylab("Temp (deg. F)")
 
grid.arrange(p1,p2, ncol=2)

1.18 Creating a Box Plot

y <- c(-5, rnorm(100), 5)
boxplot(y)

ggplot()+geom_boxplot(aes(x=factor(1),y=y))+xlab("")+ylab("")

1.19 Creating a Histogram

data(Cars93, package="MASS")
par(mfrow=c(1,2))
hist(Cars93$MPG.city)
hist(Cars93$MPG.city, 20)

p <- ggplot(Cars93, aes(MPG.city))
p1 <- p + geom_histogram(binwidth=diff(range(Cars93$MPG.city))/5)
p2 <- p + geom_histogram(binwidth=diff(range(Cars93$MPG.city))/20)
grid.arrange(p1,p2, ncol=2)

1.23 Diagnosing a Linear Regression

data(iris)
m = lm( Sepal.Length ~ Sepal.Width, data=iris)
par(mfrow=c(2,2))
plot(m)

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
r <- residuals(m)
yh <- predict(m)
scatterplot <- function(x,y,
                        title="",
                        xlab="",
                        ylab="") {
	d <- data.frame">data.frame(x=x,y=y)
	p <- ggplot(d, aes(x=x,y=y)) +
            geom_point() +
                ggtitle(title) +
                    xlab(xlab) +
                        ylab(ylab)
	return(p)
    }
p1 <- scatterplot(yh,r,
                  title="Residuals vs Fitted",
                  xlab="Fitted values",
                  ylab="Residuals")
p1 <- p1 +geom_hline(yintercept=0)+geom_smooth()
 
s <- sqrt(deviance(m)/df.residual(m))
rs <- r/s
 
qqplot <- function(y,
                   distribution=qnorm,
                   title="Normal Q-Q",
                   xlab="Theretical Quantiles",
                   ylab="Sample Quantiles") {
    require(ggplot2)
    x <- distribution(ppoints(y))
    d <- data.frame">data.frame(x=x, y=sort(y))
    p <- ggplot(d, aes(x=x, y=y)) +
        geom_point() +
            geom_line(aes(x=x, y=x)) +
                ggtitle(title=title) +
                    xlab(xlab) +
                        ylab(ylab)
    return(p)
}
 
p2 <- qqplot(rs, ylab="Standardized residuals")
 
sqrt.rs <- sqrt(abs(rs))
p3 <- scatterplot(yh,sqrt.rs,
                  title="Scale-Location",
                  xlab="Fitted values",
                  ylab=expression(sqrt("Standardized residuals")))
p3 <- p3 + geom_smooth()
 
hii <- lm.influence(m, do.coef = FALSE)$hat
p4 <- scatterplot(hii,rs)
p4 <- p4+
    geom_hline(yintercept=0)+
    geom_smooth() +
    geom_text(aes(x=min(hii)+diff(range(hii))*0.3,
                  y=min(rs)+diff(range(rs))*0.04,
                  label="--   Cook's distance", size=3))+
    theme(legend.position="none")
 
grid.arrange(p1,p2,p3,p4, ncol=2)

Related Posts

  1. :grin: thank you so much, this is really useful

    Reply

  2. 使用ggplot2画图 | YGC United States WordPress Unknow Os - pingback on June 5, 2014 at 8:03 pm
  3. I tried the Diagnosing a Linear Regression example, and it returns the following error: Error: Use 'theme' instead. (Defunct; last used in version 0.9.1)

    Reply

    ygc Hong Kong Mozilla Firefox Ubuntu Linux Reply:

    opts was defunct, and should use theme instead. I've updated the code.

    Reply

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks and Pingbacks: