First steps in Scatterplots
Standard Plot
Let’s see what you know about scatterplots in ggplot2. If you get stuck, feel free to google for hints. Good sources - reference and articles at tidyverse https://ggplot2.tidyverse.org Cookbook-R at http://www.cookbook-r.com/Graphs/ Datanovia at https://www.datanovia.com/en/?s=ggplot&search-type=default
Run the code below to glimpse the supraclavicular dataset. We are looking for continuous variables to use in scatterplots.
glimpse(supraclavicular)
Now use your knowledge to make a scatterplot of midazolam dose (x) vs. onset_motor (y)
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = ----, y = ----)) +
geom----
supraclavicular %>% filter(complete.cases(.))%>%
ggplot(x = midazolam, y = onset_motor) +
geom----
Overplotting
You can see that midazolam dose is not really continuous. It is possible that you have overlapping points with the same x and y. This is called overplotting. TO reveal whether overplotting is present, there are two common ways (and a lot of other ways https://www.data-to-viz.com/caveat/overplotting.html) to deal with this.
Alpha
one uses transparency - make your points more transparent by inserting the alpha argument into geom_point and setting alpha (0 to 1) to a low value like 0.2
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = midazolam, y = onset_motor)) +
geom_point()
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = midazolam, y = onset_motor)) +
geom_point(alpha = )
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = midazolam, y = onset_motor))+
geom_point(alpha = 0.25)
Jitter
The other approach uses randomness to move the points around - by using geom_jitter instead of geom_point. You can also set the width argument within geom_jitter to cluster the points
Edit the code below to jitter the points and set the width to 0.15. Try some other widths, and try lowering the alpha (default is alpha = 1)
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = midazolam, y = onset_motor)) +
geom_---()
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = midazolam, y = onset_motor)) +
geom_jitter(width = )
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = midazolam, y = onset_motor))+
geom_jitter(width = 0.15)
Scatterplots - mapping to color
Now we will look at the relationship between age(x) and midazolam(y) Edit the code below to produce the scatterplot, then map gender to color. Remember that gender (in this dataset) is a ordinal variable (factor).
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = --, y = --, col = )) +
geom_point()
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = age, y = midazolam, col = )) +
geom_point()
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = age, y = midazolam, col = factor(gender) )) +
geom_point()
Scatterplots - linear relationship?
Now test whether there is a relationship between age and midazolam dose. Add a best fit line with geom_smooth
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = --, y = --, col = -- )) +
geom_point() +
geom---
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = age, y = midazolam, col = factor(gender) )) +
geom_point() +
geom_smooth(method = "----")
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = age, y = midazolam, col = factor(gender) )) +
geom_point() +
geom_smooth(method = "lm")
Scatterplots - fixing icky legends
Legends often don’t have great titles, and the labels for each level often are meaningless integers. This can be fixed. Look at some hints at http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/
In this section, plot fentanyl(x) vs age (y), and map color to the anesthetic group. Then improve the legend, giving it “Anesthetic Group” for a name and Mixture and Sequential for the labels.
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes( )) +
geom_jitter() +
scale_color_discrete(name = ,
labels = )
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = fentanyl, y = age, col = factor() )) +
geom_jitter() +
scale_color_discrete(name = "Anesthetic Group",
labels = c("A", "B"))
supraclavicular %>% filter(complete.cases(.)) %>%
ggplot(aes(x = opioid_total, y = age, col = factor(group) )) +
geom_jitter() +
scale_color_discrete(name = "Anesthetic\nGroup",
labels = c("Mixture", "Sequential"))