Skip to content

Instantly share code, notes, and snippets.

@dgkeyes
Created April 26, 2019 03:44
Show Gist options
  • Save dgkeyes/065ae23da77812a402f24aa6dd7588e5 to your computer and use it in GitHub Desktop.
Save dgkeyes/065ae23da77812a402f24aa6dd7588e5 to your computer and use it in GitHub Desktop.
text and labels solutions
# Text and Labels
Copy your last code chunk. Then do the following:
1. Add text labels to each bar.
2. Use the `round` argument to just show one decimal place in each label.
3. Use the `vjust` argument to have them show up at the inner edge of the bars.
4. Make the labels all white.
```{r}
ggplot(data = sleep_by_gender,
mapping = aes(x = gender,
y = avg_sleep,
fill = gender)) +
geom_col() +
geom_text(aes(label = round(avg_sleep, 1)),
vjust = 1.5,
color = "white") +
scale_fill_brewer(palette = "Dark2",
na.value = "blue") +
scale_y_continuous(limits = c(0, 8),
breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8))
```
Do the same thing as above, but use `geom_label` instead of `geom_text`. Also, do the following:
1. Use the `vjust` argument to have them show up at the outer edge of the bars.
2. Don't show the legend (hint: you'll have to add code in two places to make this happen).
```{r}
ggplot(data = sleep_by_gender,
mapping = aes(x = gender,
y = avg_sleep,
fill = gender)) +
geom_col(show.legend = FALSE) +
geom_label(aes(label = round(avg_sleep, 1)),
vjust = -1.1,
show.legend = FALSE,
color = "white") +
scale_fill_brewer(palette = "Dark2",
na.value = "blue") +
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