Skip to content
Snippets Groups Projects
Commit c40baff3 authored by Carine Rey's avatar Carine Rey
Browse files

implement Antoine's comments

parent 1cefc855
No related branches found
No related tags found
1 merge request!6Switch to main as default branch
...@@ -247,7 +247,15 @@ ggplot(data = <DATA>) + ...@@ -247,7 +247,15 @@ ggplot(data = <DATA>) +
What happend when you use only the command `ggplot(data = mpg)` ? What happend when you use only the command `ggplot(data = mpg)` ?
</div> </div>
<details><summary>Solution</summary>
<p>
```{r only_ggplot, cache = TRUE, fig.width=4.5, fig.height=2}
ggplot(data = new_mpg)
```
</p>
</details>
<div class="pencadre"> <div class="pencadre">
Make a scatterplot of `hwy` ( fuel efficiency ) vs. `cyl` ( number of cylinders ). Make a scatterplot of `hwy` ( fuel efficiency ) vs. `cyl` ( number of cylinders ).
</div> </div>
...@@ -261,9 +269,19 @@ ggplot(data = new_mpg, mapping = aes(x = hwy, y = cyl)) + ...@@ -261,9 +269,19 @@ ggplot(data = new_mpg, mapping = aes(x = hwy, y = cyl)) +
``` ```
</p> </p>
<div class="pencadre"> <div class="pencadre">
What seems to be the problem ? What seems to be the problem ?
</div> </div>
<details><summary>Solution</summary>
<p>
Dots with the same coordinates are superposed.
</p>
</details>
</details> </details>
# Aesthetic mappings # Aesthetic mappings
...@@ -381,8 +399,8 @@ Now try to facet your plot by `fl + class` ...@@ -381,8 +399,8 @@ Now try to facet your plot by `fl + class`
Formulas allow you to express complex relationship between variables in R ! Formulas allow you to express complex relationship between variables in R !
```{r new_mpg_plot_l, cache = TRUE, fig.width=8, fig.height=4.5} ```{r new_mpg_plot_l, cache = TRUE, fig.width=8, fig.height=4.5}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() + geom_point() +
facet_wrap(~ fl + class, nrow = 2) facet_wrap(~ fl + class, nrow = 2)
``` ```
</p> </p>
...@@ -426,7 +444,7 @@ ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) + ...@@ -426,7 +444,7 @@ ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) +
\ \
We can use different `data` for different layers (you will lean more on `filter()` later) We can use different `data` (here new_mpg and mpg tables) for different layers (you will lean more on `filter()` later)
```{r new_mpg_plot_t, cache = TRUE, fig.width=8, fig.height=4.5, message=FALSE} ```{r new_mpg_plot_t, cache = TRUE, fig.width=8, fig.height=4.5, message=FALSE}
ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) + ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) +
...@@ -441,7 +459,7 @@ ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) + ...@@ -441,7 +459,7 @@ ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) +
Run this code in your head and predict what the output will look like. Then, run the code in R and check your predictions. Run this code in your head and predict what the output will look like. Then, run the code in R and check your predictions.
</div> </div>
```R ```R
ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy, color = drv)) + ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy, color = drive)) +
geom_point(show.legend = FALSE) + geom_point(show.legend = FALSE) +
geom_smooth(se = FALSE) geom_smooth(se = FALSE)
``` ```
...@@ -451,38 +469,82 @@ ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy, color = drv)) + ...@@ -451,38 +469,82 @@ ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
- What does the `se` argument to `geom_smooth()` do? - What does the `se` argument to `geom_smooth()` do?
</div> </div>
<details><summary>Solution</summary>
<p>
```{r soluce_challenge_1, cache = TRUE, fig.width=8, fig.height=4.5}
ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy, color = drive)) +
geom_point(show.legend = FALSE) +
geom_smooth(se = FALSE)
```
</p>
</details>
## Second challenge ## Second challenge
<div class="pencadre"> <div class="pencadre">
How being a `2seater` car impact the engine size versus fuel efficiency relationship ? How being a `Two Seaters` car (*class column*) impact the engine size (*displ column*) versus fuel efficiency relationship (*hwy column*) ?
1. Make a plot of `hwy` in function of `displ `
1. *Colorize* this plot in another color for `Two Seaters` class
2. *Split* this plot for each *class*
Make a plot *colorizing* this information
</div> </div>
<details><summary>Solution</summary> <details><summary>Solution 1</summary>
<p>
```{r new_mpg_plot_color_2seater1, cache = TRUE, fig.width=8, fig.height=4.5}
ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) +
geom_point()
```
</p>
</details>
<details><summary>Solution 2</summary>
<p> <p>
```{r new_mpg_plot_color_2seater, cache = TRUE, fig.width=8, fig.height=4.5} ```{r new_mpg_plot_color_2seater2, cache = TRUE, fig.width=8, fig.height=4.5}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() + geom_point() +
geom_point(data = filter(mpg, class == "2seater"), color = "red") geom_point(data = filter(new_mpg, class == "Two Seaters"), color = "red")
``` ```
</p>
</details>
<details><summary>Solution 3</summary>
<p>
```{r new_mpg_plot_color_2seater_facet, cache = TRUE, fig.width=8, fig.height=4.5}
ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_point(data = filter(new_mpg, class == "Two Seaters"), color = "red") +
facet_wrap(~class)
```
</p> </p>
</details> </details>
<div class="pencadre"> <div class="pencadre">
Write a `function` called `plot_color_2seater` that can take as sol argument the variable `mpg` and plot the same graph. Write a `function` called `plot_color_a_class` that can take as argument the class and plot the same graph for this class
</div> </div>
<details><summary>Solution</summary> <details><summary>Solution</summary>
<p> <p>
```{r new_mpg_plot_color_2seater_fx, cache = TRUE, fig.width=8, fig.height=4.5} ```{r new_mpg_plot_color_2seater_fx, cache = TRUE, fig.width=8, fig.height=4.5}
plot_color_2seater <- function(mpg) { plot_color_a_class <- function(my_class) {
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() + geom_point() +
geom_point(data = filter(mpg, class == "2seater"), color = "red") geom_point(data = filter(new_mpg, class == my_class), color = "red") +
facet_wrap(~class)
} }
plot_color_2seater(mpg) plot_color_a_class("Two Seaters")
plot_color_a_class("Compact Cars")
``` ```
</p> </p>
</details> </details>
...@@ -491,21 +553,23 @@ plot_color_2seater(mpg) ...@@ -491,21 +553,23 @@ plot_color_2seater(mpg)
## Third challenge ## Third challenge
<div class="pencadre"> <div class="pencadre">
Recreate the R code necessary to generate the following graph Recreate the R code necessary to generate the following graph (see "linetype" option of "geom_smooth")
</div> </div>
```{r new_mpg_plot_u, echo = FALSE, cache = TRUE, fig.width=8, fig.height=4.5, message=FALSE} ```{r new_mpg_plot_u, echo = FALSE, cache = TRUE, fig.width=8, fig.height=4.5, message=FALSE}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy, color = fuel)) +
geom_point() + geom_point() +
geom_smooth(mapping = aes(linetype = drv)) geom_smooth(linetype = "dashed", color = "black") +
facet_wrap(~fuel)
``` ```
<details><summary>Solution</summary> <details><summary>Solution</summary>
<p> <p>
```{r new_mpg_plot_v, eval=F} ```{r new_mpg_plot_v, eval=F}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + ggplot(data = new_mpg, mapping = aes(x = displ, y = hwy, color = fuel)) +
geom_point() + geom_point() +
geom_smooth(mapping = aes(linetype = drv)) geom_smooth(linetype = "dashed", color = "black") +
facet_wrap(~fuel)
``` ```
</p> </p>
</details> </details>
...@@ -515,7 +579,7 @@ ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + ...@@ -515,7 +579,7 @@ ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
# To go further: publication ready plots # To go further: publication ready plots
Once you have created the graph you need for your publication, you have to save it. Once you have created the graph you need for your publication, you have to save it.
You can do it with the the `ggsave` function. You can do it with the `ggsave` function.
First save your plot in a variable : First save your plot in a variable :
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment