0

For a paper I want to include a graph of my interaction effect. I am not sure if and how I could improve it to make my interaction effect more visible. This is the R code:

plot_model4 <-
  plot_model(
    model4,
    type = "int",
    terms = c("log_EU_immigration_cumulative_4yr", "lknemny"),
    ci.lvl = 0.95
    ) +
  labs(
    title = paste0("Predicted Welfare State Support Based on Exposure to EU ",
                   "Immigration and Individual Financial Insecurity"
                   ),
    x = "Cumulative EU Immigration (Log, Last 4 Years)",
    y = "Predicted Support for Welfare State",
    color = paste0("Financial Insecurity Likelihood: How likely not enough ",
                   "money for household necessities next 12 months"
                   )
    ) +
  scale_color_manual(
    values = c("#984464", "#BFA5A3", "#449777", "#A4D8A0"),
    labels = c("Not at all likely", "Not very likely", "Likely", "Very likely")
    ) +
  theme_minimal() +
  theme(legend.position = "right") +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 10))

Plot

4
  • 1
    "Improve" how, exactly. What are you trying to show that is not being shown? Commented Sep 29, 2024 at 13:31
  • @JohnGarland I was wondering if I should show all four categories or maybe only the categories "Very likely" and "Not at all likely"... Commented Sep 29, 2024 at 14:11
  • 1
    This is a minor data viz point, but it would be worth looking up how to reverse the order of the legend (from "very likely" at the top to "not at all likely" at the bottom) so that the order in the legend matches the order of the lines in the plot ... Commented Sep 29, 2024 at 20:28
  • I voted to close the question down, as it is opinion-based, lacks a clear question objective, and does not provide code for reproducibility Commented Sep 30, 2024 at 11:39

2 Answers 2

2

Unfortunately, I do not know how to achieve this with plot_model(). But here is an example with an alternative library: marginaleffects. Perhaps this will of use to you or another reader.

The marginaleffects website includes a ton of detailed tutorials. These may be particularly useful in this context:

In the first plot below, we display all categories. in the second plot, we use a list() to specify which values of the carb moderators we want to display:

library(marginaleffects)
mod <- lm(mpg ~ wt * factor(carb), data = mtcars)

plot_predictions(mod, condition = c("wt", "carb"))

plot_predictions(mod, condition = list("wt", "carb" = c(1, 8)))

Sign up to request clarification or add additional context in comments.

Comments

2

It's not an improvement to the content of your plot, but highly improves the plot readability: add line breaks to plot title and legend title:

# this part was added to use some mocked data, remove it to use your own data
library(ggplot2)
library(marginaleffects)
mod <- lm(mpg ~ wt * factor(carb), data = mtcars)

plot_predictions(mod, condition = c("wt", "carb")) +
  guides(fill="none") +
# remove the previous part up until this comment to use your own data
# and uncomment the next lines
# plot_model4 <-
#   plot_model(
#     model4,
#     type = "int",
#     terms = c("log_EU_immigration_cumulative_4yr", "lknemny"),
#     ci.lvl = 0.95
#   ) +
  labs(
    title = paste("Predicted Welfare State Support Based on Exposure to",
                  "EU Immigration and Individual Financial Insecurity",
                  sep = "\n"
    ),
    x = "Cumulative EU Immigration (Log, Last 4 Years)",
    y = "Predicted Support for Welfare State",
    color = paste("Financial Insecurity Likelihood:",
                  "How likely not enough money",
                  "for household necessities",
                  "next 12 months",
                  sep = "\n"
    )
  ) +
  scale_color_manual(
    values = c("#984464", "#BFA5A3", "#449777", "#A4D8A0"),
    labels = c("Not at all likely", "Not very likely", "Likely", "Very likely")
  ) +
  theme_minimal() +
  theme(legend.position = "right") +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 10))

plot created by the above code (with mocked data)

(Note: as you hadn't provided any data to your example code, I used the plot_predictions command from the marginaleffects library on some data from mtcars based on Vincent's answer)

2 Comments

FYI, mtcars is available in all installations of R. It doesn't require an external library.
@Vincent Thanks, it was indeed sloopy wording. I have clarified the note at the end.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.