You are on page 1of 7

One Categorical Variable

ggplot(mtcars)+

10

count
geom_bar(aes(x=cyl))
5

0
4 6 8
cyl

One Numeric Variable

10
count

gggplot(mtcars)+geom_hist(aes(x=mpg),
binwidth=5) 5

0
10 20 30 40
mpg

geom_boxplot(aes(y=mpg, x=1))+
xlab("")+coord_flip()+
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())

10 15 20 25 30 35
mpg

1
Two Numeric Variables

ggplot(mtcars)+

35
30
25

mpg
geom_point(aes(x=qsec, y=mpg))+ 20
geom_smooth(aes(x=qsec,y=mpg)) 15
10

16 18 20 22
qsec

35
30
25

mpg
geom_point(aes(x=qsec, y=mpg))+
geom_smooth(aes(x=qsec,y=mpg), 20
se=F, method="lm")
15
10
16 18 20 22
qsec

35
30 r2 = 0.16
25
mpg

geom_boxplot(aes(x=qsec, y=mpg))+
annotate(geom="text", x=21,y=32, 20
label="r^2== 0.16",parse=T)
15
10
16 18 20 22
qsec

2
One Categorical and One Numeric Variable

ggplot(mtcars)+

35
30
25

mpg
geom_boxplot(aes(x=Transmission, y=mpg)) 20
15
10
automatic manual
Transmission

0.100

0.075 Transmission

density
0.050 automatic
geom_density(aes(x=mpg, color=Transmission))
manual
0.025

0.000
10 15 20 25 30 35
mpg

automatic manual
5
4
count

geom_histogram(aes(x=mpg), binwidth=2)+ 3
facet_wrap(~Transmission) 2
1
0
10 20 30 10 20 30
mpg

30
25
mpg

geom_violin(aes(x=Transmission, y=mpg)) 20
15
10
automatic manual
Transmission

3
Two Categorical Variables
ggplot(mtcars)+

10 Transmission

count
automatic
geom_bar(aes(x=cyl, fill=Transmission ))
5 manual

0
4 6 8
cyl

12.5
10.0
Transmission

count
7.5
geom_bar(aes(x=cyl, fill=Transmission ), automatic
5.0
position="dodge") manual
2.5
0.0
4 6 8
cyl

ggMosaicplot <- function(var1, var2, name1="Var1", name2="Var2"){


levVar1 <- length(levels(var1))
levVar2 <- length(levels(var2))
jointTable <- prop.table(table(var1, var2))
plotData <- as.data.frame(jointTable)
plotData$marginVar1 <- prop.table(table(var1))
plotData$var2Height <- plotData$Freq / plotData$marginVar1
plotData$var1Center <- c(0, cumsum(plotData$marginVar1)[1:levVar1 -1]) +
plotData$marginVar1 / 2
ggplot(plotData, aes(var1Center, var2Height)) +
geom_bar(stat = "identity", aes(width = marginVar1, fill = var2)) +
scale_x_continuous(name=name1,breaks=plotData$var1Center, labels=plotData$var1, limits=c(0,1))+
theme(axis.text.x=element_text(angle=0, hjust=0, vjust=.3))+
theme(axis.title.y=element_blank())+scale_fill_discrete(name=name2)
}

1.00

0.75 Transmission

ggMosaicplot(mtcars$cyl, mtcars$Transmission, 0.50 automatic


"Number of Cylinders", "Transmission") manual
0.25

0.00
4 6 8
Number of Cylinders

4
One Categorical Variable and Two Numeric Variables

30 Transmission
ggplot(mtcars,aes(x=qsec, y=mpg,

mpg
color=Transmission))+ automatic
geom_point()+ 20 manual
geom_smooth(method="lm")
10
16 18 20 22
qsec

One Numeric Variable and Two Categorical Variables

ggplot(mtcars)+

35
30
Transmission
25
mpg

geom_boxplot(aes(x=cyl, y=mpg, automatic


fill=Transmission)) 20
manual
15
10
4 6 8
cyl

4 6 8
5

automatic
4
3
2
1
count

geom_histogram(aes(x=mpg), binwidth=3)+ 0
facet_grid(Transmission~cyl) 5
4
manual
3
2
1
0
10 20 30 40 10 20 30 40 10 20 30 40
mpg

5
Three Numeric Variables

ggplot(mtcars)+

35
hp
30
100
25 150

mpg
geom_boxplot(aes(x=qsec, y=mpg, size=hp)) 20 200
250
15
300
10
16 18 20 22
qsec

35
hp
30
300
25 250
mpg

geom_boxplot(aes(x=qsec, y=mpg, color=hp)) 200


20
150
15 100

10
16 18 20 22
qsec

Three Categorical Variables

V Engine Straight
12.5
10.0 Transmission
geom_bar(aes(x=cyl, fill=Transmission ),
count

7.5
position="dodge")+ automatic
5.0
facet_wrap(~vs) manual
2.5
0.0
4 6 8 4 6 8
cyl

6
Basic Appearance and Labels

The relationship between quarter


mile time and MPG grouped by
number of cylinders
35

ggplot(mtcars,aes(x=qsec, y=mpg, color=cyl))+ 30


geom_point()+geom_smooth(method="lm", se=F)+
xlab("Quarter Mile Time (sec)")+
ylab("MPG")+theme_bw() 25 Cylinders
scale_color_discrete(name="Cylinders",
Four

MPG
labels=c("Four", "Six", "Eight"))+
ggtitle("The elationship between quarter Six
mile time and MPG changes if you group by 20 Eight
number of cylinders")

15

10
16 18 20 22
Quarter Mile Time (sec)

You might also like