The exercises are part of the Fundamentals of R course. For more, see the R for the Rest of Us website.
Let’s load the packages we need. These include tidyverse (especially the dplyr package) and janitor.
Import your data into a data frame called NHANES. Use clean_names to make your variable names easy to work with.
Make a scatterplot that shows weight on the x axis and height on the y axis.
Make a histogram that shows the distribution of the weight variable.
Copy your code from above, but adjust it so that there are 50 bins.
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.
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.
Plot the average amount of sleep per night for males and females.
Make the same graph as above, but use geom_col instead of geom_bar.
color and fill
Take your graph from above (the one with geom_col) and make the inside of each bar a different color.
Make your scatterplot from before that shows weight on the x axis and height on the y axis again, but make the dots show up in different colors based on the phys_active variable.
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).
Do nearly the same thing to change the color of the last bar chart you made (the one about sleep and gender).
Copy the graph you just made and change the y axis so it goes from 0 to 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).
Copy your last code chunk. Then do the following:
round argument to just show one decimal place in each label.vjust argument to have them show up at the inner edge of the bars.
Do the same thing as above, but use geom_label instead of geom_text. Also, do the following:
vjust argument to have them show up at the outer edge of the bars.Use the code chunk from above with geom_text (not the last one with geom_label). Do the following:
show.legend argument again)Install and load the hrbrthemes package. 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.
Then add the theme_ipsum to your plot.
I’ve created a data frame called sleep_by_gender_by_age for you. Run the code chunk below to load the data frame.
Let’s take a look at sleep_by_gender_by_age.
Now, see if you can recreate this plot. Much of the code will be the same from your previous plots using the sleep_by_gender data frame so just make some small changes.
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”