I am following the example here: https://strengejacke.github.io/sjPlot/articles/plot_interactions.html
I copy the following:
library(sjPlot)
library(sjmisc)
library(ggplot2)
data(efc)
theme_set(theme_sjplot())
# make categorical
efc$c161sex <- to_factor(efc$c161sex)
# fit model with interaction
fit <- lm(neg_c_7 ~ c12hour + barthtot * c161sex, data = efc)
And then I do the following:
plot_model(fit, type = "pred", terms = c("c161sex", "barthtot [0, 100]"))
Why does the x-axis ticks show up as "male" and "female" when their values are 1 and 2 in the data frame?
Is there a way to replace the x-axis ticks when using plot_model
? I've been adding scale_x_discrete(breaks=c("1","2"), labels=c("male", "female"))
in other datasets and it makes my x-axis ticks disappear altogether.
And is it possible to have the lines closer to each other rather than so far apart?
When I use my own dataset (I'm unable to post the whole dataset), I get something that looks like this:
Replication code:
data = read.table(text="
X Z Y
0 1 1
1 0 1
1 0 1
0 1 1
1 1 0
1 0 0
0 0 0
1 1 1
1 1 1
0 0 1
1 0 1
1 1 1
0 1 0
1 0 0
1 0 0
0 1 1
1 1 1
1 0 1
0 0 1
1 1 1
1 1 0
0 0 0
1 0 0
1 1 1
0 1 1
1 0 1
1 0 1
0 1 1
1 1 0
1 0 0", header=TRUE)
data$X <- as.factor(data$X)
data$Z <- as.factor(data$Z)
h <- lm_robust(Y ~ X*Z, data = data)
plot_model(h, type = "pred", terms = c("X", "Z")) +
xlab("Fun Level") + ylab("") + theme_bw() +
theme(legend.title=element_blank()) +
scale_x_discrete(limits = c("Not Fun", "Fun"))
Use scale_x_discrete(limits = c("1", "2"))
; this will also bring the two categories closer together.
library(sjPlot)
#> Learn more about sjPlot with 'browseVignettes("sjPlot")'.
library(sjmisc)
library(ggplot2)
data(efc)
theme_set(theme_sjplot())
# make categorical
efc$c161sex <- to_factor(efc$c161sex)
# fit model with interaction
fit <- lm(neg_c_7 ~ c12hour + barthtot * c161sex, data = efc)
plot_model(fit, type = "pred", terms = c("c161sex", "barthtot [0, 100]")) +
scale_x_discrete(limits = c("1", "2"))
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
NEW EDIT (after additional data/info)
Somehow you should indicate, if the original post is being substantially changed. In this new addition you use a function lm_robust
which is from a package estimate`.
Key here is that you must define factor levels of data$X
: levels(data$X) <- list('Not Fun' = 0, "Fun" = 1)
. Then you can together with scale_x_discrete()
change the axis.ticks.
Within plot_model
I have used axis.lim = list(c(.6,2.4), c(-.2,1.5)
to tweak the x-axis, so that they come closer together.
library(sjPlot)
#> Learn more about sjPlot with 'browseVignettes("sjPlot")'.
library(sjmisc)
library(ggplot2)
library(estimatr)
data = read.table(text="
X Z Y
0 1 1
1 0 1
1 0 1
0 1 1
1 1 0
1 0 0
0 0 0
1 1 1
1 1 1
0 0 1
1 0 1
1 1 1
0 1 0
1 0 0
1 0 0
0 1 1
1 1 1
1 0 1
0 0 1
1 1 1
1 1 0
0 0 0
1 0 0
1 1 1
0 1 1
1 0 1
1 0 1
0 1 1
1 1 0
1 0 0", header=TRUE)
data$X <- factor(data$X)
levels(data$X) <- list('Not Fun' = 0, "Fun" = 1)
data$Z <- factor(data$Z)
h <- lm_robust(Y ~ X + Z, data = data)
plot_model(h, type ='eff', terms = c("X", "Z"),
axis.lim = list(c(.6,2.4), c(-.2,1.5))
)+
scale_x_discrete(limits = c("0" = "Not Fun", "1" = "Fun"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.