Skip to content

Instantly share code, notes, and snippets.

@dgkeyes
Created April 25, 2019 23:04
Show Gist options
  • Save dgkeyes/c5e43dbd495978e22e34fe71210e67c5 to your computer and use it in GitHub Desktop.
Save dgkeyes/c5e43dbd495978e22e34fe71210e67c5 to your computer and use it in GitHub Desktop.
bar chart solutions
# 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)) +
geom_bar()
```
## Bar Chart v2
Create a new data frame called `sleep_by_gender` that shows the average amount of sleep per night that males and females report getting. Drop any NA (or NaN) responses from this data frame.
```{r}
sleep_by_gender <- nhanes %>%
group_by(gender) %>%
summarize(avg_sleep = mean(sleep_hrs_night, na.rm = TRUE))
```
Plot the average amount of sleep per night for males and females.
```{r}
ggplot(data = sleep_by_gender,
mapping = aes(x = gender,
y = avg_sleep)) +
geom_bar(stat = "identity")
```
Make the same graph as above, but use `geom_col` instead of `geom_bar`.
```{r}
ggplot(data = sleep_by_gender,
mapping = aes(x = gender,
y = avg_sleep)) +
geom_col()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment