You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ggplot2-exercises-answers.md
+25-25Lines changed: 25 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ Once knitr is installed, you can "knit" the document by clicking the "knit" butt
18
18
First, load ggplot2:
19
19
20
20
21
-
```r
21
+
```S
22
22
library(ggplot2)
23
23
```
24
24
@@ -30,7 +30,7 @@ We're going to work with morphological data from Galapagos finches, which is ava
30
30
I've taken the data and cleaned it up a bit for this exercise. I've removed some columns and made the column names lower case. I've also removed all but one island. You can do that with this code:
31
31
32
32
33
-
```r
33
+
```S
34
34
morph <- read.csv("Morph_for_Sato.csv")
35
35
names(morph) <- tolower(names(morph)) # make columns names lowercase
36
36
morph <- subset(morph, islandid == "Flor_Chrl") # take only one island
@@ -46,7 +46,7 @@ row.names(morph) <- NULL # tidy up the row names
46
46
Take a look at the data. There are columns for taxon, sex, wing length, beak height, and upper beak length:
47
47
48
48
49
-
```r
49
+
```S
50
50
head(morph)
51
51
str(morph)
52
52
```
@@ -60,7 +60,7 @@ Then, open the ggplot2 web documentation <http://docs.ggplot2.org/> and keep it
60
60
First, let's experiment with some geoms using the `morph` dataset. I'll start by setting up a basic scatterplot of beak height vs. wing length:
@@ -72,31 +72,31 @@ Because there's lots of overplotting, I've set the alpha (opacity) value to 40%.
72
72
Experiment with ggplot2 geoms. Try applying at least 3 different geoms to the `morph` dataset. For example, try showing the distribution of wing length with `geom_histogram()` and `geom_density()`. You could also try showing the distribution of wing length for male and female birds by using `geom_violin()`. Remember to consult <http://docs.ggplot2.org/>.
@@ -110,7 +110,7 @@ Start by reading the section on aesthetics in the included notes.
110
110
Let's play with mapping some of our data to aesthetics. I'll start with one example. I'm going to map the male/female value to a colour in our scatterplot of wing length and beak height. This time I'll use jittering instead of transparency to deal with overplotting:
111
111
112
112
113
-
```r
113
+
```S
114
114
ggplot(morph, aes(wingl, beakh)) +
115
115
geom_point(aes(colour = sex),
116
116
position = position_jitter(width = 0.3, height = 0))
@@ -131,7 +131,7 @@ Some suggestions:
131
131
Yes, this last version is a bit silly, but it illustrates how quickly you can explore multiple dimensions with ggplot2.
In some cases, it's useful to let the x or y axes have different scales for each panel. Try giving each panel a different axis here using `scales = "free"` in your call to `facet_wrap()`:
As another example, let's look at the distribution of wing length by sex with different panels for each taxa. Use a boxplot or violin plot to show the distributions.
209
209
210
210
211
-
```r
211
+
```S
212
212
ggplot(morph, aes(sex, wingl)) + geom_violin() +
213
213
facet_wrap(~taxon)
214
214
```
@@ -225,7 +225,7 @@ Read the notes from the section on themes until the end.
225
225
Let's start by applying the black and white theme, `theme_bw()`. Try adding that on to the end of a plot showing the distribution of wing length by sex using `geom_violin()`. Save your plot to an object named `p` and `print()` it to show it. We'll re-use this plot in the next step.
226
226
227
227
228
-
```r
228
+
```S
229
229
p <- ggplot(morph, aes(sex, wingl)) + geom_violin() + theme_bw()
230
230
print(p)
231
231
```
@@ -236,7 +236,7 @@ print(p)
236
236
Let's go one step further and remove all grid lines. See the included notes and the help for `?theme`. Hint: setting an argument to `element_blank()` will remove it. Add these elements to the object `p` and print `p` again. Hint: see the note "Exploiting the object-oriented nature of ggplot2" in the "Random tips" section of the notes.
237
237
238
238
239
-
```r
239
+
```S
240
240
p <- p + theme(panel.grid.major = element_blank(),
241
241
panel.grid.minor = element_blank())
242
242
print(p)
@@ -248,7 +248,7 @@ print(p)
248
248
And now let's set the x and y axis labels ourselves. Name them something more appropriate.
249
249
250
250
251
-
```r
251
+
```S
252
252
p <- p + xlab("Sex") + ylab("Wing length")
253
253
print(p)
254
254
```
@@ -259,7 +259,7 @@ print(p)
259
259
Use the function `ggsave()` to save your plot to a PDF file.
ggplot2 can be useful for quickly making dot and line plots. For example, this is useful for coefficient plots. To illustrate how to make this style of plot, let's make a shows a dot for the median and line segment for the quantiles. First, we'll calculate these values. Run the following code:
Now make the plot. The final plot should have the taxa listed down the y-axis and the wing length value on the x axis. Use the geom `geom_pointrange()`. Because this geom only works for vertical line segments, you'll need to rotate the whole plot by adding `+ coord_flip()`. So, `ymax` and `ymin` refer to the maximum and minimum line segment values and `x` to the taxa, even though they will appear rotated in the end.
288
288
289
289
290
-
```r
290
+
```S
291
291
ggplot(morph_quant, aes(x = taxon, y = m, ymin = l, ymax = u)) +
292
292
geom_pointrange() + coord_flip() +
293
293
ylab("Wing length") + xlab("")
@@ -301,7 +301,7 @@ ggplot(morph_quant, aes(x = taxon, y = m, ymin = l, ymax = u)) +
301
301
ggplot2 can add model fits to the data to help visualize patterns. For example, it can quickly add linear regression lines, GLMs, GAMs, and loess curves. Let's add loess curves to scatter plots of beak height and wing length with a panel for male and female. See `?stat_smooth`
0 commit comments