Home > Net >  How can I adjust the linetype in an already existing ggplot model in R?
How can I adjust the linetype in an already existing ggplot model in R?

Time:01-28

To examine an interaction effect in my research I've created post hoc slopes using the function “emtrends” of the package emmeans (version 1.4.5.; Lenth, 2020) and visualized it in a plot using 'emmip'. I've used the code below:

emtrends(model_c, pairwise ~ Condition, var = "prsa_clean$meat_amount_c")

emmip(model_c, Condition ~ meat_amount_c, cov.reduce = range, ylab = "Favourability", xlab = "Meat consumption")

The resulting plot shows the 3 slope lines of my 3 conditions in blue/green/red lines, but doesn't adhere to the APA standards for figures, so I'm trying to adjust it using ggplot2. I manage to successfully edit the background and linesizes to my liking using the code below:

plot <- emmip(model_c, Condition ~ meat_amount_c, cov.reduce = range, ylab = "Favourability", xlab = "Meat consumption")

plot <-plot theme_bw() geom_line(size = 1.25)

However, I would like the lines to be different types. I've found the appropriate code to adjust this with ggplot is to use the function geom_line(linetype = ""). I've tried to add the code in the same way as the theme and linesize, but it doesn't work. Example tries of code below:

plot <- plot theme_bw() geom_line(size = 1.25) geom_line(linetype = "dashed")

plot <- plot theme_bw() geom_line(size = 1.25) geom_line(aes(linetype = Condition))

plot <- plot theme_bw() geom_line(size = 1.25) scale_linetype_manual(values=c("twodash", "dotted", "solid"))

I'm not a regular R user so I'm probably missing something pretty obvious. I hope someone can help me out.

In sum, I expected the geom_line(linetype="") argument to change the existing lines in the plot to become the designated types, but they remained unchanged. The theme_() and geom_line(size=) worked similarly so I expected similar results.

CodePudding user response:

Following my comment I tried some things with the emmip helpfile examples.

  • You can change line size within emmip with linearg = list(size = 1.25)
  • You can add scale_linetype_manual to the output

Example code:

library(emmeans)

noise.lm = lm(noise ~ size * type * side, data = auto.noise)

emmip(noise.lm, type ~ side * size, CIs = TRUE,
      linearg = list(size = 1.25))  
  ggplot2::scale_linetype_manual(values=c("twodash", "dotted"))

  • Related