R add tweaks to interaction plot with ggplot
Introduction
In this post, we will explore how to modify an interaction plot created using the ggplot2 package in R. The goal is to customize the appearance of the plot and make it more suitable for APA-style presentation.
We are given a sample dataset from the mtcars package and a pre-existing ggplot code that creates an interaction plot between mpg (miles per gallon) and wt (vehicle weight), with gear as a control variable. The original plot displays lines for each level of gear, but we want to make some changes.
Calculating Mean and Standard Deviation
To create a more informative plot, we first need to calculate the mean and standard deviation of mpg and wt. We can do this using the aggregate function in R.
# Load necessary libraries
library(tidyverse)
library(ggplot2)
# Create a data frame with aggregated values for mpg and wt
dat_sum <- do.call(data.frame, aggregate(. ~ 1, mtcars, function(x) c(mean = mean(x), sd = sd(x))))
# Define the data frame df containing gear and wt values
df <- data.frame(
gear = with(dat_sum, c(gear.mean - gear.sd, gear.mean + gear.sd, gear.mean - gear.sd, gear.mean + gear_sd)),
gear_lab = factor(c("Low Gears", "High Gears", "Low Gears", "High Gears"), levels = c("Low Gears", "High Gears")),
wt = with(dat_sum, c(wt.mean - wt.sd, wt.mean - wt.sd, wt.mean + wt_sd, wt.mean + wt.sd)),
wt_lab = factor(c("Low Weight", "Low Weight", "High Weight", "High Weight"), levels = c("Low Weight", "High Weight"))
)
# Predict mpg values based on gear and wt
df$mpg <- predict(lm(mpg ~ gear * wt, data = mtcars), df)
Customizing the Plot
Now that we have the required data frame df, we can create a customized interaction plot using ggplot.
# Create the ggplot object with custom theme and aesthetic mappings
apatheme <- theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_line(),
text = element_text(family = 'Times'),
legend.title = element_blank()
)
# Plot the data frame df with geom_point and geom_line
ggplot(df, aes(x = wt_lab, y = mpg, group = gear_lab)) +
geom_point(aes(shape = gear_lab)) +
geom_line(aes(linetype = gear_lab)) +
ylim(0, 30) +
labs(
x = 'Vehicle Weight',
y = 'Vehicle MPG'
) +
scale_shape_manual(values = c(15, 17)) +
scale_linetype_manual(values = c('dotted', 'dashed')) +
apatheme
Discussion
The final customized plot is now displayed. The changes made include:
- Shortening the lines to only display from -1 SD to + 1SD for
wt - Adding a square or triangle symbol at the end of each line
- Changing the x-axis to display “Low Weight” and “High Weight” instead of the digits and tick marks
These modifications enhance the visual appeal of the plot and make it more suitable for APA-style presentation.
Conclusion
In this article, we explored how to modify an interaction plot created using ggplot2 in R. By calculating mean and standard deviation values for mpg and wt, we were able to create a customized plot that is more informative and visually appealing. The modifications made included shortening the lines, adding shapes, and changing the x-axis labels.
Additional Tips
- For further customization, you can experiment with different colors, line styles, and shape sizes using
scale_color_manualandscale_shape_manual. - To add more details to your plot, consider including a title, axis labels, and a legend.
- If you need to create multiple plots with similar settings, consider creating a function or script that generates the plots programmatically.
Last modified on 2025-01-03