Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions episodes/visualizing-ggplot.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,19 @@ ggplot(data = complete_old, mapping = aes(x = plot_type, y = hindfoot_length)) +
geom_jitter(alpha = 0.2)
```

You may have noticed that some of our data points are now appearing on our plot twice: the outliers are plotted as black points from `geom_boxplot()`, but they are also plotted with `geom_jitter()`. Since we don't want to represent these data multiple times in the same form (points), we can stop `geom_boxplot()` from plotting them. We do this by setting the `outlier.shape` argument to `NA`, which means the outliers don't have a shape to be plotted.
You may have noticed that some of our data points are now appearing on our plot twice: the outliers are plotted as black points from `geom_boxplot()`, but they are also plotted with `geom_jitter()`. Since we don't want to represent these data multiple times in the same form (points), we can stop `geom_boxplot()` from plotting them. We do this by setting the `outliers` argument to `FALSE`.

```{r boxplot-outliers, warning = F}
ggplot(data = complete_old, mapping = aes(x = plot_type, y = hindfoot_length)) +
geom_boxplot(outlier.shape = NA) +
geom_boxplot(outliers = FALSE) +
geom_jitter(alpha = 0.2)
```

Just as before, we can map `plot_type` to `color` by putting it inside `aes()`.

```{r global-color, warning=FALSE}
ggplot(data = complete_old, mapping = aes(x = plot_type, y = hindfoot_length, color = plot_type)) +
geom_boxplot(outlier.shape = NA) +
geom_boxplot(outliers = FALSE) +
geom_jitter(alpha = 0.2)
```

Expand All @@ -341,7 +341,7 @@ If we want to limit the mapping to a single `geom`, we can put the mapping into

```{r geom-color, warning=FALSE}
ggplot(data = complete_old, mapping = aes(x = plot_type, y = hindfoot_length)) +
geom_boxplot(outlier.shape = NA) +
geom_boxplot(outliers = FALSE) +
geom_jitter(aes(color = plot_type), alpha = 0.2)
```

Expand All @@ -350,15 +350,15 @@ Now our points are colored according to `plot_type`, but the boxplots are all th
```{r reverse-layers, warning=FALSE}
ggplot(data = complete_old, mapping = aes(x = plot_type, y = hindfoot_length)) +
geom_jitter(aes(color = plot_type), alpha = 0.2) +
geom_boxplot(outlier.shape = NA)
geom_boxplot(outliers = FALSE)
```

Now we have the opposite problem! The white `fill` of the boxplots completely obscures some of the points. To address this problem, we can remove the `fill` from the boxplots altogether, leaving only the black lines. To do this, we set `fill` to `NA`:

```{r fill-na, warning=FALSE}
ggplot(data = complete_old, mapping = aes(x = plot_type, y = hindfoot_length)) +
geom_jitter(aes(color = plot_type), alpha = 0.2) +
geom_boxplot(outlier.shape = NA, fill = NA)
geom_boxplot(outliers = FALSE, fill = NA)
```

Now we can see all the raw data and our boxplots on top.
Expand Down Expand Up @@ -411,7 +411,7 @@ We will create an object called `myplot`. If you run the name of the `ggplot2` o
```{r}
myplot <- ggplot(data = complete_old, mapping = aes(x = plot_type, y = hindfoot_length)) +
geom_jitter(aes(color = plot_type), alpha = 0.2) +
geom_boxplot(outlier.shape = NA, fill = NA)
geom_boxplot(outliers = FALSE, fill = NA)

myplot
```
Expand Down Expand Up @@ -471,7 +471,7 @@ Because there are so many possible arguments to the `theme()` function, it can s

You may have noticed that we have used 3 different approaches to getting rid of something in `ggplot`:

- `outlier.shape = NA` to remove the outliers from our boxplot
- `outliers = FALSE` to remove the outliers from our boxplot
- `panel.grid.major.x = element_blank()` to remove the x grid lines
- `legend.position = "none"` to remove our legend

Expand Down
Loading