strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science

Home Page:https://strengejacke.github.io/sjPlot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cut off regression line to remove impossible combinations

syl-vie opened this issue · comments

Dear Daniel Lüdecke, thank you for developing strengejacke.
I have the following issue: I test how insects per plot are affected by plot tree species richness and origin. The plot I get via plot_model is nice, but it includes also a regression line for the impossible combination between species_richness = 1 and plot_status = "mixed".
Is there a way how I can let the regression line for "mixed" start only at species_richness = 2? I know that for base r it is possible to "clip" lines. Is there a similar function in sjPlot?
Thank you in advance.
This is a reprex:
library(sjPlot)
species_richness <- c(1,1,2,2,2,4,4,4,6,6,6)
plot_status <- c("native", "exotic","mixed","native", "exotic","mixed","native", "exotic","mixed","native", "exotic")
insect.pplot <- c(12,1,7,13,1,6,15,1,5,17,2)
df <- data.frame(species_richness, plot_status, herbivore.pplot)
m1 <- glm(insect.pplot ~ species_richness * plot_status, data=df)
plot_model(m1, type = "eff", terms=c("species_richness","plot_status"))

I suggest using ggeffects, which is internally used by sjPlot, but which is a bit more flexible. The plot() method has a limit.range argument, which does what you want.

library(ggeffects)

species_richness <- c(1, 1, 2, 2, 2, 4, 4, 4, 6, 6, 6)
plot_status <- c("native", "exotic", "mixed", "native", "exotic", "mixed",
                 "native", "exotic", "mixed", "native", "exotic")
insect.pplot <- c(12, 1, 7, 13, 1, 6, 15, 1, 5, 17, 2)
df <- data.frame(species_richness, plot_status)
m1 <- glm(insect.pplot ~ species_richness * plot_status, data = df)

p <- ggemmeans(m1, c("species_richness", "plot_status"))
#> Loading required namespace: emmeans
plot(p, limit.range = TRUE)
#> Loading required namespace: ggplot2

Created on 2021-10-28 by the reprex package (v2.0.1)

Dear @strengejacke thank you for showing this elegant way of plotting regression lines only in their respective range limit.