The goal of this practical is to practices advanced features of `ggplot2`.
# Introduction
In the last session, we have seen how to use `ggplot2` and [The Grammar of Graphics](https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448/ref=as_li_ss_tl). The goal of this practical is to practices more advanced features of `ggplot2`.
The objectives of this session will be to:
...
...
@@ -49,43 +34,31 @@ The objectives of this session will be to:
- practices position adjustments
- change the coordinate systems
\
# `ggplot2` statistical transformations
\
The first step is to load the `tidyverse`.
<details><summary>Solution</summary>
<p>
```{r packageloaded, include=TRUE, message=FALSE}
library("tidyverse")
```
</p>
</details>
\
Like in the previous sessions, it's good practice to create a new **.R** file to write your code instead of using directly the R terminal.
# `ggplot2` statistical transformations
We are going to use the `diamonds` data set included in `tidyverse`.
- Use the `help` and `view` command to explore this data set.
- Try the `str` command, which information are displayed ?
```R
```{r str_diamon}
str(diamonds)
```
```
## Classes 'tbl_df', 'tbl' and 'data.frame': 53940 obs. of 10 variables:
Every geom has a default stat; and every stat has a default geom. This means that you can typically use geoms without worrying about the underlying statistical transformation. There are three reasons you might need to use a stat explicitly:
- You might want to override the default stat.
...
...
@@ -124,14 +95,15 @@ demo <- tribble(
"Premium", 13791,
"Ideal", 21551
)
```
# (Don't worry that you haven't seen <- or tribble() before. You might be able
# to guess at their meaning from the context, and you will learn exactly what
# they do soon!)
(Don't worry that you haven't seen `tribble()` before. You might be able
to guess at their meaning from the context, and you will learn exactly what
If group is not used, the proportion is calculated with respect to the data that contains that field and is ultimately going to be 100% in any case. For instance, The proportion of an ideal cut in the ideal cut specific data will be 1.
\
- You might want to draw greater attention to the statistical transformation in your code.
you might use stat_summary(), which summarises the y values for each unique x
value, to draw attention to the summary that you are computing:
ggplot(data = diamonds, mapping = aes(x = cut, y = depth, color = clarity)) +
geom_violin()
```
# Coordinate systems
Cartesian coordinate system where the x and y positions act independently to determine the location of each point. There are a number of other coordinate systems that are occasionally helpful.