• Home
  • University of Lincoln
  • Data Skills for Life Sciences
  • Visualizing Relationships with Multiple Continuous Explanatory Variables

Visualizing Relationships with Multiple Continuous Explanatory Variables

12 June 2021 18:38 Chapter 6 MULTIPLE CONTINUOUS EXPLANATORY VARIABLES There are three different possible combinations of explanatory variables: · One categorical, one numeric . Two categorical · Two numeric ADDITIVE RELATIONSHIPS = o Two quantities can be expressed as related to each other through addition. o The effect of one quantity adds to the effect of the other but the two effects don't modify each other. INTERACTIVE RELATIONSHIPS = o This means that the effect of one variable depends on the effect of the other. o The situation in which two or more objects or events act upon one another to produce a new effect We used ggplot with geom_point() to visualise a numeric explanatory (x) variable against a numeric response (y) variable. In order to colour the points by role, all that is needed is to put aes(colour = role) inside the geom_point() function scatter <- ggplot(data, aes(x = eyesight, y = reaction)) + geom_point(aes(colour = role)) scatter 0.8 - With just the points, it's difficult to understand what this means for the relationship between eyesight and reaction. Adding a fitted line makes this clearer. This means a statistics layer to the plot using stat_smooth(). 0.6 - reaction role . baby . child . . person staff · method = "Im" means a straight line is plotted. "Im" stands for "linear model", which is the type of statistical model we'll be using later in the course. . se = FALSE instructs not to plot standard error margins; just the fitted line. 0.4- 0.2- -5 student .. . 0 5 eyesight 10 15 scatter <- ggplot(data, aes(x = eyesight, y = reaction)) + geom_point(aes(colour = role)) + stat_smooth(method = "Im", se = FALSE) scatter 0.8- . By specifying the fitted lines to also be coloured by role, we can see this: 0.6 - reaction scatter <- ggplot(data, aes(x = eyesight, y = reaction)) + geom_point(aes(colour = role)) scatter + stat_smooth(method = "Im", se = FALSE, aes(colour = role)) 0.4- role baby -child person staff - student GROUPING POINTS BY SHAPE 0.2- -5 5 10 15 eyesight To get points with a different shape, simply change the shape aesthetic, rather than the colour aesthetic: scatter <- ggplot(data, aes(x = eyesight, y = reaction)) + geom_point(aes(shape = role)) scatter COMBINING GROUPING METHODS It's possible to combine different ways of defining points by adding variable = variable2 into the aes of geom_point(). scatter <- ggplot(data, aes(x = eyesight, y = reaction)) + geom_point(aes(colour = role, variable = variable2)) scatter + stat_smooth(method = "Im", se = FALSE, aes(colour = role)) Facets - which split a plot into subplots. facet_grid() specifies rows and columns of subplots to create. It's necessary to use the vars() function with facet_grid() ! facet_grid(cols = vars(role)) MEAN AND STANDARD ERROR, BOXPLOTS, AND VIOLIN PLOTS Mean and standard error bars are used in the example above, but the same approaches for faceting and colouring apply to boxplots and violin plots. The same approach applies: · facet_grid() creates facets o use