简体   繁体   中英

Editing the y axis on a sjplot in R (plot_model)

I am having trouble with my y axis on this sjplot I have created. I am not sure why the values are arranged like that (see image) and was wondering if anyone could help me eg set my y axis to start at 0.

library(jtools)
library(carData)
library(effects)
library(sjPlot)
mod <- glmer(Golden.Trevally ~ Maturity.Status + Behavioural.Activity + (1 | ID.Number), family = "binomial", data = mydf2)
summary(mod)
plot_model(mod, "pred", title="")

见图片/情节

By far the hardest part of answering this question was recreating your data to make it reproducible. However, the following is pretty close:

library(jtools)
library(carData)
library(effects)
library(sjPlot)
library(lme4)

set.seed(69)

Behavioural.Activity <- factor(sample(c("Cleaning", "Courtship", 
                                        "Cruising", "Feeding"),
                                        size = 10000, 
                                        replace = TRUE))
Maturity.Status <- factor(sample(LETTERS[1:3], 10000, TRUE))
ID.Number <- factor(sample(500, 10000, TRUE))
Golden.Trevally <- rbinom(10000, 1, prob =
                          (c(6, 4, 7, 3)/600)[as.numeric(Behavioural.Activity)] *
                          c(0.8, 1, 1.2)[as.numeric(Maturity.Status)] *
                          (as.numeric(ID.Number) / 1000 + 0.75))

mydf2 <- data.frame(ID.Number, Golden.Trevally, 
                    Behavioural.Activity, Maturity.Status)

mod <- glmer(Golden.Trevally ~ Maturity.Status + Behavioural.Activity + (1 | ID.Number), 
             family = "binomial", data = mydf2)

my_sjplot <- plot_model(mod, "pred", title = "")

my_sjplot$Behavioural.Activity

在此处输入图像描述

The solution here is to realize that the object returned by plot_model is a list containing two ggplot objects. You are seeing the one for Behavioural.Activity . It looks the way it does because it has a scale_y_continuous whose labelling function is labelling the breaks to the nearest percent. You can simply over-ride this scale with one of your own:

my_sjplot$Behavioural.Activity +
   scale_y_continuous(limits = c(0, 0.01), 
                      labels = scales::percent_format(accuracy = 0.01))

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM