-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtt1.Rmd
205 lines (193 loc) · 8.76 KB
/
tt1.Rmd
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
title: "Lecture 1 Some R Base Plotting"
output:
html_document:
toc: true
toc_float: true
toc_depth: 4
---
```{r}
#devtools::install_github("heike/extracat")
library("ggthemes")
library("GGally")
library("extracat")
library(hdrcde)
library(KernSmooth)
library("ggplot2")
library("gridExtra")
library("vcd")
```
```{r}
# The Titanic data revisited
Titanic1 <- data.frame(Titanic)
head(Titanic1)
str(Titanic1)
p <- ggplot(Titanic1, aes(weight=Freq)) + ylab("") + ylim(0,2250)
cs <- p + aes(Class) + geom_bar(fill="blue")
sx <- p + aes(Sex) + geom_bar(fill="green")
ag <- p + aes(Age) + geom_bar(fill="tan2")
su <- p + aes(Survived) + geom_bar(fill="red")
grid.arrange(cs, sx, ag, su, nrow=1, widths=c(3, 2, 2, 2))
doubledecker(Survived ~ Sex, data = Titanic, gp = gpar(fill = c("grey90", "blue")))
doubledecker(Survived ~ Sex, data = Titanic, gp = gpar(fill = c("grey90", "blue")))
doubledecker(Survived ~ Class, data = Titanic, gp = gpar(fill = c("grey90", "blue")))
doubledecker(Survived ~ Sex + Class, data = Titanic, gp = gpar(fill = c("grey90", "blue")))
doubledecker(Survived ~ Class + Age, data = Titanic, gp = gpar(fill = c("grey90", "blue")))
doubledecker(Survived ~ Sex + Class + Age, data = Titanic, gp = gpar(fill = c("grey90", "blue")))
```
```{r}
# Movies data
#install.packages("ggplot2movies")
library(ggplot2movies)
data(movies)
#?movies
ggplot(movies, aes(length)) + geom_bar() + ylab("") + xlab("Movie length (minutes)")
ggplot(movies, aes("var", length)) + geom_boxplot() + xlab("") +
ylab("Movie length (minutes)") + scale_x_discrete(breaks=NULL) + coord_flip()
ggplot(movies, aes(length)) + ylab("") + xlim(0,180) +
geom_histogram(binwidth=1) +
xlab("Movie length (minutes)")
ggplot(movies, aes(x = length)) + xlim(0,240) +
geom_histogram(binwidth=1) +
xlab("Movie length (minutes)") + ylab("")
ggplot(movies, aes(x = length)) + xlim(0,240) +
geom_histogram(aes(y=..density..),binwidth=1) +
xlab("Movie length (minutes)") + ylab("") +geom_density(colour="blue")
ggplot(movies, aes(votes, rating,alpha=0.005)) + geom_point(size=0.25) + ylim(1,10) + ylab("Rating") + xlab("Votes")
foo<-which(movies$votes>10000)
length(foo)
ggplot(movies[foo,], aes(votes, rating)) + geom_point() + ylim(1,10) + ylab("Rating") + xlab("Votes")
foo1<-which(movies$rating[foo]==min(movies$rating[foo]))
movies1<-movies[foo,]
movies1[foo1,]
foo2<-which(movies$rating[foo]==max(movies$rating[foo]))
movies2<-movies[foo,]
movies2[foo2,]
temp<-sort(movies2$rating,decreasing=TRUE,index.return=TRUE)
temp$ix
movies2[temp$ix,]
movies2[temp$ix,1]
summary(movies$year)
ggplot(movies, aes(length, rating)) + geom_point() + ylim(1,10) + ylab("Rating") + xlab("Length (minutes)")+ xlim(60,240)
# Berkeley data
ucba <- as.data.frame(UCBAdmissions)
a <- ggplot(ucba, aes(Dept)) + geom_bar(aes(weight=Freq))
b <- ggplot(ucba, aes(Gender)) + geom_bar(aes(weight=Freq))
c <- ggplot(ucba, aes(Admit)) + geom_bar(aes(weight=Freq))
grid.arrange(a, b, c, nrow=1, widths=c(7,3,3))
ucba <- within(ucba, Accept <- factor(Admit, levels=c("Rejected", "Admitted")))
doubledecker(xtabs(Freq~ Dept + Gender + Accept, data = ucba), gp = gpar(fill = c("grey90", "steelblue")))
#Geyser Data
library(hdrcde)
data(geyser, package="MASS")
ggplot(geyser, aes(duration, waiting)) + geom_point() + ylab("Waiting time (minutes)") + xlab("Duration (minutes)")
ggplot(geyser, aes(duration, waiting)) + geom_point() + geom_density2d() + ylab("Waiting time (minutes)") + xlab("Duration (minutes)")
```
```{r}
data(father.son, package="UsingR")
c2 <- ggplot(father.son, aes(sheight)) +
geom_histogram(aes(y = ..density..), binwidth=1) +
geom_density(colour="blue") + xlim(58, 80) + ylim(0, 0.16) +
xlab("Height (inches)") + ylab("") + ggtitle("Sons")
p2 <- ggplot(father.son, aes(fheight)) +
geom_histogram(aes(y = ..density..), binwidth=1) +
geom_density(colour="blue") + xlim(58, 80) + ylim(0, 0.16) +
xlab("Height (inches)") + ylab("") +
ggtitle("Fathers")
grid.arrange(c2, p2, nrow = 1)
qqnorm(father.son$sheight, main="Sons", xlab="",ylab="", pch=16, ylim=c(55,80))
qqline(father.son$sheight)
qqnorm(father.son$fheight, main="Fathers", xlab="",ylab="", pch=16, ylim=c(55,80))
qqline(father.son$fheight)
ggplot(father.son, aes(fheight, sheight)) + geom_point() +
geom_smooth(method="lm", colour="red", se=FALSE) +
geom_abline(slope=1, intercept=0) +xlab("Father's height (inches)")+ylab("Son's height (inches)")
ggplot(father.son, aes(fheight, sheight)) + geom_point() +
geom_smooth(method="lm", colour="red", se=TRUE) +
geom_abline(slope=1, intercept=0) +xlab("Father's height (inches)")+ylab("Son's height (inches)")
ggplot(father.son, aes(fheight, sheight)) + geom_point() +
geom_smooth(method="lm", colour="red", se=FALSE) +
stat_smooth() +xlab("Father's height (inches)")+ylab("Son's height (inches)")
# Iris data
data(iris)
ggpairs(iris, aes(colour=Species, alpha=0.4))
ggparcoord(iris, columns=1:4, groupColumn="Species")
a <- ggplot(iris, aes("Boxplot for all", Sepal.Width)) +
xlab("") + geom_boxplot() +
scale_x_discrete(breaks=NULL)
b <- ggplot(iris, aes(Species, Sepal.Width)) +
geom_boxplot() + xlab("")
grid.arrange(a, b, nrow=1, widths=c(1,2))
a <- ggplot(iris, aes("Boxplot for all", Petal.Width)) +
xlab("") + geom_boxplot() +
scale_x_discrete(breaks=NULL)
b <- ggplot(iris, aes(Species, Petal.Width)) +
geom_boxplot() + xlab("")
grid.arrange(a, b, nrow=1, widths=c(1,2))
# Body data
data(body, package="gclus")
body1 <- body
names(body1) <- abbreviate(names(body), 2)
names(body1)[c(4:5, 11:13, 19:21)] <-
c("CDp", "CD", "Ch", "Ws", "Ab", "Cl", "An", "Wr")
a1 <- ggparcoord(body1, columns=1:24, alphaLines=0.1,groupColumn="Gn") + xlab("") + ylab("")
a2 <- ggparcoord(body1, columns=1:24, scale="uniminmax", alphaLines=0.1) + xlab("") + ylab("")
a3 <- ggparcoord(body1, columns=1:24, scale="globalminmax", alphaLines=0.1) + xlab("") + ylab("")
a4 <- ggparcoord(body1, columns=1:24, scale="center",scaleSummary="median", alphaLines=0.1) +xlab("") + ylab("")
grid.arrange(a1, a2, a3, a4)
a1 <- ggparcoord(body1, columns=1:24, alphaLines=0.1,groupColumn="Gn") + xlab("") + ylab("")
a1
body1$Gn<-as.factor(body1$Gn)
a1 <- ggparcoord(body1, columns=1:24, alphaLines=0.3,groupColumn="Gn") +xlab("") + ylab("")
a1
a1 <- ggparcoord(body1, columns=1:24, alphaLines=0.1,groupColumn="Gn") + xlab("") + ylab("")
a2 <- ggparcoord(body1, columns=1:24, scale="uniminmax",groupColumn="Gn",alphaLines=0.1) + xlab("") + ylab("")
a3 <- ggparcoord(body1, columns=1:24,scale="globalminmax", alphaLines=0.1,groupColumn="Gn") + xlab("") + ylab("")
a4 <- ggparcoord(body1, columns=1:24, scale="center",scaleSummary="median", alphaLines=0.1,groupColumn="Gn") +xlab("") + ylab("")
grid.arrange(a1, a2, a3, a4)
# Coffee data
data(coffee, package="pgmm")
coffee <- within(coffee, Type <- ifelse(Variety==1,"Arabica", "Robusta"))
names(coffee) <- abbreviate(names(coffee), 8)
ggpairs(coffee[,-c(1,2)], aes(colour=Type, alpha=0.4))
names(coffee)[6]<-"Ph"
ggpairs(coffee[,-c(1,2)], aes(colour=Type, alpha=0.4))
a <- ggplot(coffee, aes(x=Type)) + geom_bar(aes(fill=Type)) +
scale_fill_manual(values = c("grey70", "red")) +
guides(fill=FALSE) + ylab("")
b <- ggplot(coffee, aes(x=Fat, y=Caffine, colour=Type)) +
geom_point(size=2) +
scale_colour_manual(values = c("grey70", "red"))
c <- ggparcoord(coffee[order(coffee$Type),], columns=3:14,
groupColumn="Type", scale="uniminmax") +
xlab("") + ylab("") +
theme(legend.position = "none") +
scale_colour_manual(values = c("grey","red")) +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank())
grid.arrange(arrangeGrob(a, b, ncol=2, widths=c(1,2)), c, nrow=2)
# Housing data
library(vcd)
data(housing, package="MASS")
?housing
mosaic(xtabs(Freq ~ Cont + Type + Infl + Sat, data = housing),
direction = c("h", "v", "v", "h"),
gp = gpar(fill = c("grey", "grey","blue")),
spacing = spacing_highlighting)
par(mar=c(5, 4, 4, 2) + 0.1)
rmb(formula = ~Type+Cont+Infl+Sat, data = housing, cat.ord = 3,
spine = TRUE, freq.trans = "const")
# Crime data
data(crime.us, package="VGAMdata")
crime1<-crime.us
names(crime1)<-gsub("*Rate","",names(crime1))
names(crime1)[19:20]<-c("Larceny","MVTheft")
ggpairs(crime1[,c(13:16,18:20)],diag=list(continuous="densityDiag"),axisLabels="none")
# Boston data
data(Boston, package="MASS")
par(mar=c(1.1, 1.1, 1.1, 1.1))
palette(rainbow(14, s = 0.6, v = 0.75))
stars(Boston[1:12,], labels=NULL, draw.segments = TRUE)
stars(Boston, labels=NULL, draw.segments = TRUE)
par(mar=c(5, 4, 4, 2) + 0.1)
```