-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvis01_ggplot.qmd
185 lines (118 loc) · 5.83 KB
/
vis01_ggplot.qmd
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
# ggplot2 basics
> Es gibt nur eine Breitbandleitung ins Hirn und das sind die Augen. [David Kriesel](https://www.youtube.com/watch?v=QqVFEP3_BtY)
In the course, we used a variety of datasets. All of them have different features and difficulties that we need to keep in mind when we want to visualize parts of the data to point out some interesting facts!
The **German Climate Data** data has a temporal component and "continuous" measurements. The **arthoropode species on crop fields** follow the classic plot based design and allows for comparisons between different groups. The **trees and districts of Muenster** potentially have a spatial component to it but also allow for comparisons of the different species and districts.
Fortunately, there is a common language to visualize most datasets and their specific attributes: the grammar of graphics.
## The grammar of graphics in R
Scientific figures and advanced visualization in R are usually done with the `ggplot2` package.
* [https://ggplot2.tidyverse.org/](https://ggplot2.tidyverse.org/)
* tidyverse package: synergies and integration with e.g. dplyr
* layer based language that are combined with `+`
* differentiated by "static" and "dynamic" parameters
* many extensions: [https://exts.ggplot2.tidyverse.org/gallery/](https://exts.ggplot2.tidyverse.org/gallery/)
* Gallery for inspiration: [https://r-graph-gallery.com/index.html](https://r-graph-gallery.com/index.html)
```{r}
library(ggplot2)
library(viridis) # for better color scales
species = read.csv("data/crop_species.csv")
```
### Initialze the plot
Tell ggplot which `data.frame` is the basis for the plot.
```{r}
ggplot(data = species)
```
The `mapping` argument of the `ggplot()` function handles which columns of the data should be used for which purpose.
To specify this, we use the `aes()` function (aesthetics). Common things that we specify:
* x
* y
* group
* color
* fill
Here we specify that on the x-axis in our plot represent the `AraInd` column. As a result we get the proper y-axis, but still no data is shown. For this, we have to specify in which form the data should be depicted.
```{r}
ggplot(data = species, mapping = aes(x = AraInd))
```
### Geometry types
There are many different geometry types build into `ggplot2` and even more from additional packages. The functions to create the geometry layers usually start with `geom_`. The geom functions also have their own arguments you can specify.
* geom_point
* geom_line
* geom_boxplot
* geom_histogram
* ...
```{r}
ggplot(species, aes(x = AraInd))+
geom_histogram()
```
```{r}
ggplot(species, aes(x = AraInd))+
geom_histogram(binwidth = 10, fill = "darkgreen", color = "black")
```
Different geometry types require more than one aesthetic. E.g. `geom_point` needs the x and y aesthetics.
```{r}
ggplot(species, aes(x = Croptype, y = AraInd))+
geom_point()
```
### Axis Formats
The Axis format depends on the type of variable you want to plot. We have to be aware of what scale type we have.
* discrete scale for categorical data
* continuous scale for numeric data
* limits, ticks, labels ...
```{r}
ggplot(species, aes(x = Croptype, y = AraInd))+
geom_point(size = 4, alpha = 0.8, shape = 18)+
scale_x_discrete(labels = c("KE" = "Grain Pea",
"SM" = "Silage Maize",
"WR" = "Winter Oilseed Rape",
"WW" = "Winter Wheat",
"ZR" = "Sugar Beet"))+
scale_y_continuous(name = "Aranae [n]", limits = c(0,200))
```
We can also save the plot to a object to reuse and modify it later.
```{r}
araind_plot = ggplot(species, aes(x = Croptype, y = AraInd))+
geom_point(size = 4, alpha = 0.8, shape = 18)+
scale_x_discrete(labels = c("KE" = "Grain Pea",
"SM" = "Silage Maize",
"WR" = "Winter Oilseed Rape",
"WW" = "Winter Wheat",
"ZR" = "Sugar Beet"))+
scale_y_continuous(name = "Aranae [n]", limits = c(0,200))
araind_plot + ggtitle("Aranae [n] per crop type")
```
### Color Scales
With the `color` and `fill` arguments in `aes()` we specify which columns of the data.frame should be represented as a color scale in the plot.
```{r}
ggplot(species, aes(x = Croptype, y = AraInd, color = AraSpec))+
geom_point(size = 4, alpha = 0.8, shape = 18)+
scale_x_discrete(name = "Crop Type")+
scale_y_continuous(name = "Aranae [n]", limits = c(0,200))
```
We can specify the colors by adding a `scale_color_` layer. Just like with the axis, there are discrete and continuous color scales available!
```{r}
ggplot(species, aes(x = Croptype, y = AraInd, color = AraSpec))+
geom_point(size = 4, alpha = 0.8, shape = 18)+
scale_color_gradient(name = "Aranae\nSpecies [n]", low = "yellow", high = "red")+
scale_x_discrete(name = "Crop Type")+
scale_y_continuous(name = "Aranae [n]", limits = c(0,200))
```
### Themes
There are many prebuild themes...
```{r}
ggplot(species, aes(x = Croptype, y = AraInd, color = AraSpec))+
geom_point(size = 4, alpha = 0.8, shape = 18)+
scale_color_gradientn(name = "Aranae\nSpecies [n]",colors = mako(50))+
scale_x_discrete(name = "Crop Type")+
scale_y_continuous(name = "Aranae [n]", limits = c(0,200))+
theme_minimal()
```
... or you can specify your own:
```{r}
ggplot(species, aes(x = Croptype, y = AraInd, color = AraSpec))+
geom_point(size = 4, alpha = 0.8, shape = 18)+
scale_color_gradientn(name = "Aranae\nSpecies [n]",colors = mako(50))+
scale_x_discrete(name = "Crop Type")+
scale_y_continuous(name = "Aranae [n]", limits = c(0,200), expand = c(0,0))+
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "black", linetype = "dotted"),
axis.line = element_line(color = "black"))
```