-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGGplotTutorial.R
More file actions
executable file
·199 lines (133 loc) · 4.95 KB
/
GGplotTutorial.R
File metadata and controls
executable file
·199 lines (133 loc) · 4.95 KB
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
library(ggplot2)
##Here is the data
data(iris)
## Being Hasty?
plot(iris$Sepal.Length, iris$Species)
plot(iris$Species, iris$Sepal.Length)
#Look at the Data
summary(iris)
#Base Plot;
##Pros: quick, simple X-Y, simple colors
##Cons: simple, less than beautiful
plot(iris$Sepal.Width, iris$Petal.Length)
plot(iris$Sepal.Width, iris$Petal.Length, col='pink')
plot(iris$Sepal.Width, iris$Petal.Length, col='pink', pch=15)
plot(iris$Sepal.Width, iris$Petal.Length, col=iris$Species, pch=15)
class(iris$Species)
iris$Species
class(iris$Species)
str(iris)
quartz()
plot(
iris$Sepal.Width,
iris$Petal.Length,
col = as.numeric(iris$Species),
pch = 15,
xlab = "Sepal Width",
ylab = "Petal Length"
)
## Base Plot lets you add things onto the original plot, all within the plotting
## window. In general, this is great. But it can be a problem if you want to add
## or extract or edit deeper pieces
legend(
"right",
legend = levels(iris$Species),
col = 1:length(levels(iris$Species)),
pch = 15
)
lines(iris$Sepal.Width,
iris$Petal.Length,type = 'p' )
title(main='Sepal vs Petal by Species')
## But. instead, lets let the amazing magicians over at ggplot do it for us!
library(ggplot2)
## The first call, 'ggplot( ... )' is to set up the 'window' which you will plot
## in
ggplot()
ggplot(data = iris, aes())
## WHAT?? NOTHING HAPPENED?
## Why yes, thats simply because we have to make it 'aesthetic;)'
ggplot(data = iris, aes(Sepal.Width, Petal.Length))
## Something almost happened. Can you tell?
## Now we just need to add what we want the plot to DO. Scatter? Line? Box?
ggplot(data = iris,aes(Sepal.Width, Petal.Length))+geom_point()
## But kind sir, that is no different than the original
## I beg to differ;
ggplot(data = iris,aes(x=Sepal.Width, y=Petal.Length, colour = Species))+geom_point()
# Look how easy it is to add fun things! all via AES (cant spell EASY without AES)
ggplot(data = iris,aes(x=Sepal.Width, y=Petal.Length, colour = Species))+
geom_point()+geom_smooth()
ggplot(data = iris,aes(y=Sepal.Width, fill = Species, group = Species))+
geom_boxplot()
#Now lets do something fun
## WARNING. WHICHES AHEAD
iris2=iris
iris2$SepalCat = NA
iris2$SepalCat[which(iris2$Sepal.Width>=3.25)]='Large'
iris2$SepalCat[which(iris2$Sepal.Width<3.25)]='Small'
table(iris2$SepalCat)
ggplot(data=iris2, aes(x=SepalCat,y=Petal.Length, fill = Species))+
geom_boxplot()+
ggpubr::stat_compare_means(method = 't.test',label = 'p.signif',
comparisons = list(c("Large","Small")))
#####
# OWN DATA
library(ggpubr)
ggplot(data = iris,aes(x=Species, y=Sepal.Width, fill = Species, group = Species))+
geom_boxplot()+
ggtitle(label = "Boxplot Prettyness")+
theme(plot.title = element_text(face='bold', hjust = 0.5))+
scale_fill_manual(values = c('lightgoldenrod', 'salmon2', '#11E700'))+
ggpubr::stat_compare_means(method = 't.test',label = 'p.signif',
comparisons = list(c("setosa","versicolor"),
c("setosa", "virginica"),
c("virginica", "versicolor")))
?ggpubr::stat_compare_means
?ggpubr::stat_pvalue_manual()
p_vals=data.frame('group1'=c('setosa', 'setosa', 'virginica'),
'group2'=c('versicolor', 'virginica', 'versicolor'),
'p'=c('***', 'N---S', 'NS'),'y.position'=c(5,6,4))
ggplot(data = iris,aes(x=Species, y=Sepal.Width, fill = Species, group = Species))+
geom_boxplot()+
ggtitle(label = "Boxplot Prettyness")+
theme(plot.title = element_text(face='bold', hjust = 0.5))+
ggpubr::stat_pvalue_manual(data=p_vals)+ggpubr::theme_pubr()
iris2$Species_SepalCat <- interaction(
iris2$Species,
iris2$SepalCat,
sep = "_"
)
ggplot(iris2,
aes(x = Species_SepalCat,
y = Petal.Length,
fill = Species)) +
geom_boxplot() +
stat_compare_means(
comparisons = list(
c("setosa_Small", "versicolor_Small"),
c("setosa_Large", "setosa_Small")
),
method = "wilcox.test",
label = "p.signif"
)+
geom_boxplot() +
scale_x_discrete(
labels = function(x) {
ifelse(grepl("versicolor_Small", x), "Small",
ifelse(grepl("versicolor_Large", x), "Large", ""))
}
)
# Thats great for categorical, but maybe not so much for contionous:
ggplot(data = iris,aes(y=Petal.Width, x=Sepal.Width, shape = Species))+
geom_point(aes(colour=Petal.Length))+
scale_colour_gradientn(colours = c('blue', 'white','red'))
ggplot(data = iris,aes(y=Petal.Width, x=Sepal.Width, shape = Species))+
geom_point(aes(color=Petal.Length), size=3)+
scale_color_gradient(low = 'chartreuse', high = 'violetred1')+
scale_shape_manual(values = c(15,16,17))
colors()
"mediumvioletred"
"linen"
"#F0F"
"mediumaquamarine"
"#A33000"
"lavenderblush"