Skip to content

Commit 4d09578

Browse files
committed
Switch to S code chunks for Github highlighting
1 parent db12b65 commit 4d09578

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

ggplot2-exercises-answers.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Once knitr is installed, you can "knit" the document by clicking the "knit" butt
1818
First, load ggplot2:
1919

2020

21-
```r
21+
```S
2222
library(ggplot2)
2323
```
2424

@@ -30,7 +30,7 @@ We're going to work with morphological data from Galapagos finches, which is ava
3030
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:
3131

3232

33-
```r
33+
```S
3434
morph <- read.csv("Morph_for_Sato.csv")
3535
names(morph) <- tolower(names(morph)) # make columns names lowercase
3636
morph <- subset(morph, islandid == "Flor_Chrl") # take only one island
@@ -46,7 +46,7 @@ row.names(morph) <- NULL # tidy up the row names
4646
Take a look at the data. There are columns for taxon, sex, wing length, beak height, and upper beak length:
4747

4848

49-
```r
49+
```S
5050
head(morph)
5151
str(morph)
5252
```
@@ -60,7 +60,7 @@ Then, open the ggplot2 web documentation <http://docs.ggplot2.org/> and keep it
6060
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:
6161

6262

63-
```r
63+
```S
6464
ggplot(morph, aes(wingl, beakh)) + geom_point(alpha = 0.4)
6565
```
6666

@@ -72,31 +72,31 @@ Because there's lots of overplotting, I've set the alpha (opacity) value to 40%.
7272
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/>.
7373

7474

75-
```r
75+
```S
7676
ggplot(morph, aes(wingl)) + geom_histogram(binwidth=1)
7777
```
7878

7979
![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-71.png)
8080

81-
```r
81+
```S
8282
ggplot(morph, aes(wingl)) + geom_density()
8383
```
8484

8585
![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-72.png)
8686

87-
```r
87+
```S
8888
ggplot(morph, aes(sex, wingl)) + geom_violin()
8989
```
9090

9191
![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-73.png)
9292

93-
```r
93+
```S
9494
ggplot(morph, aes(taxon, wingl)) + geom_violin() + coord_flip() # coord_flip() rotates 90 degrees
9595
```
9696

9797
![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-74.png)
9898

99-
```r
99+
```S
100100
ggplot(morph, aes(taxon, wingl)) + geom_boxplot() + coord_flip()
101101
```
102102

@@ -110,7 +110,7 @@ Start by reading the section on aesthetics in the included notes.
110110
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:
111111

112112

113-
```r
113+
```S
114114
ggplot(morph, aes(wingl, beakh)) +
115115
geom_point(aes(colour = sex),
116116
position = position_jitter(width = 0.3, height = 0))
@@ -131,7 +131,7 @@ Some suggestions:
131131
Yes, this last version is a bit silly, but it illustrates how quickly you can explore multiple dimensions with ggplot2.
132132

133133

134-
```r
134+
```S
135135
ggplot(morph, aes(wingl, beakh)) +
136136
geom_point(aes(size = ubeakl), alpha = 0.4)
137137
```
@@ -140,7 +140,7 @@ ggplot(morph, aes(wingl, beakh)) +
140140

141141

142142

143-
```r
143+
```S
144144
ggplot(morph, aes(wingl, beakh)) +
145145
geom_point(aes(colour = taxon),
146146
position = position_jitter(width = 0.3, height = 0))
@@ -150,15 +150,15 @@ ggplot(morph, aes(wingl, beakh)) +
150150

151151

152152

153-
```r
153+
```S
154154
ggplot(morph, aes(wingl, beakh)) +
155155
geom_point(aes(shape = sex),
156156
position = position_jitter(width = 0.3, height = 0))
157157
```
158158

159159
![plot of chunk unnamed-chunk-11](figure/unnamed-chunk-11.png)
160160

161-
```r
161+
```S
162162
ggplot(morph, aes(wingl, beakh)) +
163163
geom_point(aes(shape = sex, size = ubeakl, colour = taxon),
164164
alpha = 0.4)
@@ -174,7 +174,7 @@ Read the notes section on small multiples.
174174
Try a scatterplot of beak height against wing length with a different panel for each taxon. Use `facet_wrap`:
175175

176176

177-
```r
177+
```S
178178
ggplot(morph, aes(wingl, beakh)) +
179179
geom_point(alpha = 0.4) + facet_wrap(~taxon)
180180
```
@@ -185,7 +185,7 @@ ggplot(morph, aes(wingl, beakh)) +
185185
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()`:
186186

187187

188-
```r
188+
```S
189189
ggplot(morph, aes(wingl, beakh)) +
190190
geom_point(alpha = 0.4) +
191191
facet_wrap(~taxon, scales = "free")
@@ -197,7 +197,7 @@ ggplot(morph, aes(wingl, beakh)) +
197197
Now try using `facet_grid` to explore the same scatterplot for each combination of sex and taxa. (Remove the `scales = "free"` code for simplicity.)
198198

199199

200-
```r
200+
```S
201201
ggplot(morph, aes(wingl, beakh)) +
202202
geom_point(alpha = 0.4) + facet_grid(sex~taxon)
203203
```
@@ -208,7 +208,7 @@ ggplot(morph, aes(wingl, beakh)) +
208208
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.
209209

210210

211-
```r
211+
```S
212212
ggplot(morph, aes(sex, wingl)) + geom_violin() +
213213
facet_wrap(~taxon)
214214
```
@@ -225,7 +225,7 @@ Read the notes from the section on themes until the end.
225225
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.
226226

227227

228-
```r
228+
```S
229229
p <- ggplot(morph, aes(sex, wingl)) + geom_violin() + theme_bw()
230230
print(p)
231231
```
@@ -236,7 +236,7 @@ print(p)
236236
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.
237237

238238

239-
```r
239+
```S
240240
p <- p + theme(panel.grid.major = element_blank(),
241241
panel.grid.minor = element_blank())
242242
print(p)
@@ -248,7 +248,7 @@ print(p)
248248
And now let's set the x and y axis labels ourselves. Name them something more appropriate.
249249

250250

251-
```r
251+
```S
252252
p <- p + xlab("Sex") + ylab("Wing length")
253253
print(p)
254254
```
@@ -259,7 +259,7 @@ print(p)
259259
Use the function `ggsave()` to save your plot to a PDF file.
260260

261261

262-
```r
262+
```S
263263
ggsave(p, file = "wingl-violin.pdf", width = 6, height = 6)
264264
```
265265

@@ -271,7 +271,7 @@ ggsave(p, file = "wingl-violin.pdf", width = 6, height = 6)
271271
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:
272272

273273

274-
```r
274+
```S
275275
# install.packages("dplyr")
276276
library(dplyr)
277277
morph_quant <- as.data.frame(summarise(group_by(morph, taxon),
@@ -287,7 +287,7 @@ morph_quant <- transform(morph_quant,
287287
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.
288288

289289

290-
```r
290+
```S
291291
ggplot(morph_quant, aes(x = taxon, y = m, ymin = l, ymax = u)) +
292292
geom_pointrange() + coord_flip() +
293293
ylab("Wing length") + xlab("")
@@ -301,7 +301,7 @@ ggplot(morph_quant, aes(x = taxon, y = m, ymin = l, ymax = u)) +
301301
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`
302302

303303

304-
```r
304+
```S
305305
ggplot(morph, aes(wingl, beakh)) +
306306
geom_point(alpha = 0.4) + facet_wrap(~sex) +
307307
stat_smooth(method = "loess")

makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ notes:
66

77
eg:
88
Rscript -e "knitr::knit('ggplot2-exercises-answers.Rmd')"
9+
perl -p -i -e "s/\`\`\`r/\`\`\`S/g" ggplot2-exercises-answers.md

0 commit comments

Comments
 (0)