Home > Blockchain >  R plot_model - Change both font size and legend labels
R plot_model - Change both font size and legend labels

Time:02-05

I created a plot of an interaction using plot_model. The problem is that plot_model doesn't have a way to change the legend labels, so I had to add that using scale_color_manual. But that seems to override the changes I want to make to the size of the axis labels.

The data file df1 looks like this:

  X  Groups   DV        SK
1 1 Group 1 5.00 1.1813822
2 2 Group 1 1.00 1.1813822
3 3 Group 1 2.00 1.1813822
4 4 Group 1 1.75 0.2678133
5 5 Group 1 3.75 0.2678133
6 6 Group 1 2.50 0.7245978
...
360 360 Group 3 6.00  0.2678133
361 361 Group 3 1.75 -1.5593244
362 362 Group 3 3.25 -0.6457555
363 363 Group 3 5.25 -1.1025399
364 364 Group 3 3.00 -2.9296776
365 365 Group 3 2.75  0.2678133

And here is my attempt to create the plot:

library(sjPlot)
library(ggplot2)

fit1 = lm(DV~Groups*SK, data=df1)

gg_colors <- c("blue", "red")

p1 <- plot_model(fit1, type = "pred", terms = c("Groups", "SK[-0.6457555 ,0.7245978]"), title= "", axis.title = c("","Y TITLE"), legend.title="SK")
p1   
  scale_color_manual(labels = c("25 Percentile", "75th Percentile"), values=gg_colors)   font_size(axis_title.y = 12, labels.x=12)   legend_style(pos="top")

The output looks like this: plot

The problem is that the font size didn't change to 12 as I specified in the code. Ideally I'd also like to change the axis x labels to black instead of grey.

CodePudding user response:

This might be most clearly handled using a theme element and giving a global text size:

library(sjPlot)
#> Install package "strengejacke" from GitHub (`devtools::install_github("strengejacke/strengejacke")`) to load all sj-packages at once!
library(tidyverse)

df1 <- tibble(Groups = rep(c("Group 1", "Group 2", "Group 3"), each = 100),
       DV = rnorm(300, 5),
       SK = rnorm(300, 0, 2))

fit1 <-  lm(DV~Groups*SK, data=df1)


gg_colors <- c("blue", "red")

p1 <- plot_model(fit1, type = "pred", terms = c("Groups", "SK[-0.6457555 ,0.7245978]"), title= "", axis.title = c("","Y TITLE"), legend.title="SK")
p1   
  scale_color_manual(labels = c("25 Percentile", "75th Percentile"), values=gg_colors)   
  theme(text = element_text(size = 14),
        axis.text = element_text(colour = "black"),
        legend.position = "top")
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.

  • Related