Skip to content

Instantly share code, notes, and snippets.

View dgkeyes's full-sized avatar

David Keyes dgkeyes

View GitHub Profile
@dgkeyes
dgkeyes / ggplot-with-arguments.R
Last active May 8, 2019 16:20
ggplot with arguments
ggplot(data = sleep_by_gender,
mapping = aes(x = gender,
y = avg_sleep,
fill = gender)) +
geom_col()
@dgkeyes
dgkeyes / save-plots-solutions.R
Last active April 26, 2019 16:43
save plots solutions
# 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")
@dgkeyes
dgkeyes / facets-solutions.R
Created April 26, 2019 14:24
facets solutions
# 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()
```
@dgkeyes
dgkeyes / themes-solutions.R
Created April 26, 2019 14:13
themes solutions
# 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)
@dgkeyes
dgkeyes / plot-labels-solutions.R
Created April 26, 2019 03:52
plot labels solutions
# 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}
@dgkeyes
dgkeyes / text-labels-solutions.R
Created April 26, 2019 03:44
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}
@dgkeyes
dgkeyes / scales-solutions.R
Created April 26, 2019 03:36
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,
@dgkeyes
dgkeyes / color-fill-solutions.R
Created April 26, 2019 03:27
color and fill solutions
# `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()
@dgkeyes
dgkeyes / bar-chart-solutions.R
Created April 25, 2019 23:04
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)) +
@dgkeyes
dgkeyes / histogram-solutions.R
Created April 25, 2019 22:11
histogram solutions
# Histogram
Make a histogram that shows the distribution of the weight variable.
```{r}
ggplot(data = nhanes,
mapping = aes(x = weight)) +
geom_histogram()
```