library(tidyverse)
#Load datasets we will use for the lecture
source(here::here("code", "healthcare.R")) #loads dataset-specific variables

Lets take a glimpse at our dataset called heart


Basics of a ggplot code

Below is an example of the most basic form of the ggplot code

ggplot(data)+ #this will generate a blank plot for your data. At this point, ggplot knows what to plot, but not how.
  geom(mapping=aes(x, y)) #adding a geom tells ggplot how you would like to map your data

Use this template to make a simple ggplot.

  1. Use the heart dataset,
  2. Make a scatterplot comparing age vs. maximum heart rate Recall that a "data dictionary" is located here: https://github.com/matthewhirschey/tidybiology-plusds

Next, fit a smooth line (linear regression) to these points *by adding another geom and specifying the argument method = "lm"


Next, color by whether the patient has exercise-induced angina


The last plot probably has your color based on a continuous scale; now change color by sex (a discrete variable)


Next, fix the labels to capitalize, and be more informative


Next, give the plot a title, by calling title = "title" within labs And give the plot a caption, by calling caption = "Plot by YOURNAME" within labs


Next, use facet_grid to split this graph based on whether the pateint has exercise induced angina or not. Make sure to wrap your variable in factor() to convert the numeric into a factor variable.


Next, change the theme to light


Lastly, save the object as a png image using ggsave




BAREJAA/reactivity documentation built on April 16, 2020, 6:57 p.m.