Reputation: 25
I need to customize my plot with plot_model, a function for plotting generalised linear mixed effect models. I have both a forest plot showing coefficients and a line plot showing probabilities.
In the forest plot below, I have the Age factor with two levels (young adults, older adults). What I would like to achieve is to change the point estimate shapes for the two groups, e.g., showing a circle for the young adults and a diamond for the older adults. In the figure below I would like to have a different shape for "Age:New", "Age:Lure", "Age:Old". However, I do not find any extra argument that allows me to do so. Any Idea?
Forest Plot
Similarly, in the line plot below, I would like to change the facet_grid labels from 0 and 1 to "Young adults" and "Older adults". This is achievable with ggplot2 but as I used sjplot I would like to know if there is a way to implement it in the same in it.
Line plot
I have already plotted my models, however I would like to achieve some extra flexibility.
This is the code for the forest plot:
plot_model(model1_original_conf, type = "est", transform = NULL, terms = c("ZcConfusabilitySimWeightedSim",
"Age1:ZcConfusabilitySimWeightedSim",
"Conditionlure:ZcConfusabilitySimWeightedSim",
"Conditionold:ZcConfusabilitySimWeightedSim",
"Age1:Conditionlure:ZcConfusabilitySimWeightedSim",
"Age1:Conditionold:ZcConfusabilitySimWeightedSim"),
colors = c("seagreen3", "deepskyblue2", "orchid"),
show.values = TRUE,
value.offset = .4,
value.size = 6,
dot.size = 6,
line.size = 2.5,
vline.color = "red",
width = 0.5,
axis.labels = c("Age:New", "New", "Age:Lure", "Lure", "Age:Old", "Old"),
title = "Semantic Confusability",
order.terms = c(4,6,3,5,1,2),
group.terms = c(1,1,2,3,2,3))
This is the code for the line plot:
plot_model(model1_original_conf, type = "pred",
terms = c("ZcConfusabilitySimWeightedSim", "Condition", "Age"),
colors = c("seagreen3", "deepskyblue2", "orchid"),
title = "Raw probabilities",
axis.title = c("Semantic Confusability", "p (judge item is OLD)"),
line.size = 2.0)
I expect to find 1) different shapes for the point estimates in the forest plot (e.g., circles and triangles or diamonds) and 2) appropriate labels for 0 and 1 for the line plots ("young adults" and "older adults").
Upvotes: 0
Views: 2318
Reputation: 7832
However, I do not find any extra argument that allows me to do so. Any Idea?
You need ggplot2::aes()
to update the mapping, and then you can add scale_shape_manual()
to the plot that is returned from plot_model()
.
Example:
library(sjPlot)
library(ggplot2)
m <- lm(Sepal.Length ~ Petal.Width + Petal.Length + Species, data = iris)
plot_model(m, group.terms = c(1, 1, 2, 2)) +
aes(shape = group) +
scale_shape_manual(values = c(1, 4))
Created on 2019-05-06 by the reprex package (v0.2.1)
I would like to change the facet_grid labels from 0 and 1 to "Young adults" and "Older adults".
This should work when you have a factor with the required factor levels:
library(sjPlot)
library(ggplot2)
library(sjmisc)
set.seed(123)
iris$binary <- sample(c(0, 1), nrow(iris), replace = T)
iris$Sepal.Length_d <- sjmisc::dicho(iris$Sepal.Length)
m2 <- glm(Sepal.Length_d ~ Petal.Width + Petal.Length * Species * binary, data = iris, family = binomial())
plot_model(m2, type = "pred", terms = c("Petal.Length", "Species", "binary"), ci.lvl = NA)
iris$binary <- sjmisc::rec(iris$binary, rec = "0=young adults;1=older adults", as.num = FALSE)
m3 <- glm(Sepal.Length_d ~ Petal.Width + Petal.Length * Species * binary, data = iris, family = binomial())
plot_model(m3, type = "pred", terms = c("Petal.Length", "Species", "binary"), ci.lvl = NA)
Created on 2019-05-06 by the reprex package (v0.2.1)
Upvotes: 3