Commit 2994d0ab authored by Bernd Klaus's avatar Bernd Klaus

finished scatterplot example, now on to regression

parent 7aa47425
......@@ -117,9 +117,21 @@ tra_detected <- filter(mtec_counts_tidy, is_detected == TRUE,
tra_detected
## ----tra_vs_all----------------------------------------------------------
ggplot(tra_detected, aes(x = total_detected, y = tra))+
geom_point() +
coord_equal()
scatter_tra <- ggplot(tra_detected, aes(x = total_detected, y = tra))+
geom_point() +
coord_equal()
scatter_tra
## ----more_layers---------------------------------------------------------
scatter_tra +
geom_rug(alpha = I(0.2))
## ----ex_geom-------------------------------------------------------------
ggplot(tra_detected, aes(x = total_detected, y = tra))
scatter_tra +
geom_smooth(method = "lm")
## ----session_info, cache = FALSE-----------------------------------------
sessionInfo()
......
......@@ -156,7 +156,7 @@ load(file.path(data_dir, "tras.RData"))
`r CRANpkg("ggplot2")` is a package by Hadley Wickham that implements the idea of
*grammar of graphics* -- a concept created by Leland Wilkinson in his book of the same name. Comprehensive documentation for the package can
be found on <http://docs.ggplot2.org>. The online documentation includes
be found on <http://ggplot2.tidyverse.org/>. The online documentation includes
example use cases for each of the graphic types that are introduced in this lab (and
many more) and is an invaluable resource when creating figures.
......@@ -214,9 +214,58 @@ tra_detected
We visualize this relationship in a scatterplot with ggplot2.
```{r tra_vs_all}
ggplot(tra_detected, aes(x = total_detected, y = tra))+
geom_point() +
coord_equal()
scatter_tra <- ggplot(tra_detected, aes(x = total_detected, y = tra))+
geom_point() +
coord_equal()
scatter_tra
```
We just wrote our first "sentence" using the grammar of graphics.
Let us deconstruct this sentence.
First, we specified the dataframe that contains the data, `mtec_counts_tidy`.
Then we told `ggplot` via the `aes`. This stands for `aesthetic` argument
(Aesthetics are visual property of the objects in your plot.)
and tells `r CRANpkg("ggplot2")` which variables
we want on the \(x\)-- and \(y\)--axes, respectively.
Finally, we stated that we want the plot to use points, by adding the result
of calling the function `geom_point`. The plot would be good to go now, but
as both axes represent counts and thus have the same units,
we want their aspect ratio to be equal.
Note that in `r CRANpkg("ggplot2")`, we build a plot "layer by layer":
We first specify a mapping from the data to aesthetics and then use an
appropriate geometry to display it. We can always add new layers or modify
existing ones by literally adding them, here we add horizontal lines
indicating our marginal counts to the scatter plot. This is known
as a "rug plot"
```{r more_layers}
scatter_tra +
geom_rug(alpha = I(0.2))
```
We set (instead of mapped) the alpha value to 0.2, this increaseses the
transparancy of the rugs and alleviates overplotting.
# Exercise: Geometries and aspect ratios
1. What happens if you omit the calls to `geom_point()` and
`coord_equal()` ?
2. Use the internet (e.g. <http://ggplot2.tidyverse.org/>) to find
out how to add a smoothing curve to the plot. Can you change the smoother
to a regression line?
```{r ex_geom}
ggplot(tra_detected, aes(x = total_detected, y = tra))
scatter_tra +
geom_smooth(method = "lm")
```
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment