-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdplyr_ggplot2.R
More file actions
244 lines (152 loc) · 5.49 KB
/
Copy pathdplyr_ggplot2.R
File metadata and controls
244 lines (152 loc) · 5.49 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
### Agenda
# 1. Wczytywanie pakietów w R
# 2. Wczytywanie i zapisywanie danych z pliku
# 3. Podstawy pakietu dplyr
# 4. Podstawy wizualizacji z pakietem ggplot2
################################################################################
# 1. Wczytywanie pakietów w R
#instalacja
if(FALSE) {
install.packages("dplyr")
install.packages("ggplot2")
}
#wczytywanie
library(dplyr) #przetwarzanie danych
library(ggplot2) #wizualizacja
detach("package:ggplot2", unload = TRUE)
library(ggplot2)
################################################################################
# 2. Wczytywanie i zapisywanie danych z pliku
## wczytywanie danych
iris_data <- read.csv("iris.csv", header = TRUE)
read.table("iris.csv", head = TRUE, sep = ",", dec = ",") #a także read.csv2(...) - domyślny separator to ";"
## wyświetlanie danych
head(iris_data)
head(iris_data, n = 10)
tail(iris_data, n = 8)
## zmiana nazw kolumn
colnames(iris_data) <- c("Id", "Sepal_Length", "Sepal_Width",
"Petal_Length", "Petal_Width", "Species")
## zapisywanie danych
write.csv(iris_data, "iris_new_columns.csv") #write.table(...), write.csv2(...)
################################################################################
# 3. Podstawy pakietu dplyr
## znak pipe %>%
?dim
dim(iris_data)
iris_data %>% dim() # pipe przekazuje obiekt z lewej strony do funkcji z prawej
###### Pytanie: Co będzie wynikiem poniższego wywołania?
iris_data %>%
dim() %>%
sum()
######
## Funkcja select(...)
select(iris_data, Sepal_Length, Sepal_Width)
iris_data %>%
select(Sepal_Length, Sepal_Width)
all.equal(select(iris_data, Sepal_Length, Sepal_Width),
iris_data %>% select(Sepal_Length, Sepal_Width)) # to samo
iris_data %>%
select( -Species, -Id) %>%
colMeans() # funkcja z bazowego R
# funkcja filter()
iris_data %>%
filter(Species == "setosa", Sepal_Length >= 5) #przecinek oznacza to samo co &
iris_data %>%
filter(Species == "setosa", Sepal_Length >= 5) %>%
nrow()
##############
#### PYTANIE: Ile jest w ramce danych kwiatów mających szerokość płatka (Petal_Width) większą od 0.2 i mniejszą od 1?
iris_data %>%
filter(Petal_Width > 0.2, Petal_Width < 1) %>%
nrow()
##############
## funkcja mutate(...)
iris_data %>%
mutate(Petal_area = Petal_Length*Petal_Width)
set.seed(17) #ustawianie ziarna losowości
colours <- sample(x = c("red", "blue", "yellow"),
prob = c(0.1, 0.6, 0.3),
size = nrow(iris_data),
replace = TRUE)
iris_colours <- iris_data %>%
mutate(Petal_area = Petal_Length*Petal_Width,
Colours = colours) # można tworzyć kilka kolumn jednocześnie
## grupowanie funkcją group_by(...)
iris_colours %>%
group_by(Species) # 3 grupy dla każdego gatunku
iris_colours %>%
group_by(Species, Colours) # 9 grup (3 gatunki x 3 kolory)
iris_colours %>%
group_by(Species, Colours) %>%
ungroup()
iris_colours %>%
group_by(Colours) %>%
mutate(mean_Petal_Length = mean(Petal_Length))
## funkcje summarise(...) i summarise_all(...)
# przykład ze zliczaniem grupowanych obserwacji
iris_colours %>%
group_by(Species) %>%
summarise(n = n())
iris_colours %>%
group_by(Colours) %>%
summarise(n = n())
# agregacja
iris_colours %>%
group_by(Species) %>%
filter(Sepal_Length >= 5) %>%
summarise(sd_Petal_Length = sd(Petal_Length))
#### Pytanie: JAka jest średnia szerokość płatka (Petal_Width) dla yellow setosa?
iris_colours %>%
group_by(Colours, Species) %>%
summarise(mean_Petal_Width = mean(Petal_Width))
###############################################
head(iris_colours)
iris_colours %>%
select(-Id, -Colours) %>%
group_by(Species) %>%
summarise_all(.funs = sd)
iris_colours %>%
summarise_all(.funs = mean) # wartości NA dla kolumn o złym typie
colMeans(iris_colours) # błąd wywołania
################################################################################
# 4. Podstawy wizualizacji z pakietem ggplot2
## wykresy punktowe
ggplot()
ggplot(iris_colours, aes(x = Petal_Length, y = Petal_Width))
ggplot(iris_colours, aes(x = Petal_Length, y = Petal_Width)) +
geom_point()
ggplot() +
geom_point(iris_colours, mapping = aes(x = Petal_Length, y = Petal_Width))
ggplot(iris_colours, aes(x = Petal_Length, y = Petal_Width, col = Species)) +
geom_point()
ggplot(iris_colours, aes(x = Petal_Length, y = Petal_Width, col = Species)) +
geom_point() +
ggtitle("Nice plot title here!") + # dodanie tytułu
theme_bw() # zmiana stylu wykresu
## histogramy:
iris_plot <- iris_data %>%
group_by(Species) %>%
mutate(mean_Petal_Length = mean(Petal_Length))
# pipe przekazuje także dane do funkdji ggplot
iris_plot %>%
ggplot(aes(x = Petal_Length, fill = Species)) +
geom_histogram(binwidth = 1)
iris_plot %>%
ggplot(aes(x = Petal_Length)) +
geom_histogram(binwidth = 1)+
facet_wrap(~Species, scales = "free_y") # dzielimy wykres na osobne panele,
#manipulujemy skalami przy użyciu parametru scales
# dodawanie kolejnego mappingu
ggplot(iris_plot, aes(x = Petal_Length)) +
geom_histogram(binwidth = 1) +
facet_wrap(~Species, scales = "free_y") +
geom_vline(iris_plot, mapping = aes(xintercept = mean_Petal_Length, col = Species), size = 1)
## wykres słupkowy
iris_colours %>%
ggplot(aes(x = Colours, fill = Colours)) +
geom_bar()
iris_colours %>%
ggplot() +
geom_bar(aes(x = Colours, fill = Colours)) +
scale_fill_manual(values = c("blue", "red", "gold"))