This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(data = sleep_by_gender, | |
mapping = aes(x = gender, | |
y = avg_sleep, | |
fill = gender)) + | |
geom_col() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Save Plots | |
Save your last plot to a PNG that is 8 inches wide and 5 inches high. Put it in the plots directory and call it "my-sleep-plot.png" | |
```{r} | |
ggsave(filename = "plots/my-sleep-plot.png", | |
height = 5, | |
width = 8, | |
unit = "in") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Facets | |
I've created a data frame called `sleep_by_gender_by_age` for you. Run the code chunk below to load the data frame. | |
```{r} | |
sleep_by_gender_by_age <- nhanes %>% | |
group_by(gender, age_decade) %>% | |
summarize(avg_sleep = mean(sleep_hrs_night, na.rm = TRUE)) %>% | |
drop_na() | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Themes | |
Install and load the [`hrbrthemes` package](https://hrbrmstr.github.io/hrbrthemes/index.html). It's a package that provides some great default themes. | |
It's not available on CRAN, where we normally install packages from, which means you have to install it slightly differently. You'll use the `devtools` package and then use this to install the `hrbrthemes` package. Use the code below. | |
```{r} | |
# install.packages("devtools") | |
# devtools::install_github("hrbrmstr/hrbrthemes") | |
library(hrbrthemes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plot Labels | |
Use the code chunk from above with `geom_text` (not the last one with `geom_label`). Do the following: | |
1. Add a title | |
2. Add a better y axis label | |
3. Remove the x axis label | |
4. Remove the legend (hint: use the `show.legend` argument again) | |
```{r} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# `color` and `fill` | |
Take your graph from above (the one with `geom_col`) and make the inside of each bar a different color. | |
```{r} | |
ggplot(data = sleep_by_gender, | |
mapping = aes(x = gender, | |
y = avg_sleep, | |
fill = gender)) + | |
geom_col() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bar Chart | |
## Bar Chart v1 | |
Use the v1 approach to make a bar chart that shows a count of the number of people who say they smoke. Include NA responses. | |
```{r} | |
ggplot(data = nhanes, | |
mapping = aes(x = smoke_now)) + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Histogram | |
Make a histogram that shows the distribution of the weight variable. | |
```{r} | |
ggplot(data = nhanes, | |
mapping = aes(x = weight)) + | |
geom_histogram() | |
``` |