Skip to content

Instantly share code, notes, and snippets.

@dgkeyes
Created April 26, 2019 03:36
Show Gist options
  • Save dgkeyes/e06e7f16fcdd21986bb3e18f1343cbd7 to your computer and use it in GitHub Desktop.
Save dgkeyes/e06e7f16fcdd21986bb3e18f1343cbd7 to your computer and use it in GitHub Desktop.
scales solutions
# Scales
## color
Take your scatterplot that you just made and add a scale using `scale_color_brewer`. Take a look at the help docs and choose a palette other than the default (hint: look at the `palette` argument).
```{r}
ggplot(data = nhanes,
mapping = aes(x = weight,
y = height,
color = phys_active)) +
geom_point() +
scale_color_brewer(palette = "Dark2")
```
Do nearly the same thing to change the color of the last bar chart you made (the one about sleep and gender).
```{r}
ggplot(data = sleep_by_gender,
mapping = aes(x = gender,
y = avg_sleep,
fill = gender)) +
geom_col() +
scale_fill_brewer(palette = "Dark2")
```
## x and y axes
Copy the graph you just made and change the y axis so it goes from 0 to 8.
```{r}
ggplot(data = sleep_by_gender,
mapping = aes(x = gender,
y = avg_sleep,
fill = gender)) +
geom_col() +
scale_fill_brewer(palette = "Dark2") +
scale_y_continuous(limits = c(0, 8))
```
Copy the last code chunk. Then adjust the breaks on the y axis so that it shows 0 to 8 by 1 (i.e. 0, 1, 2, etc).
```{r}
ggplot(data = sleep_by_gender,
mapping = aes(x = gender,
y = avg_sleep,
fill = gender)) +
geom_col() +
scale_fill_brewer(palette = "Dark2") +
scale_y_continuous(limits = c(0, 8),
breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment