Jana
Jana

Reputation: 19

How to improve my plot for my interaction model

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

Upvotes: 0

Views: 75

Answers (2)

oszkar
oszkar

Reputation: 992

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)

Upvotes: 2

Vincent
Vincent

Reputation: 17823

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)))

Upvotes: 2

Related Questions