I am trying to make plots of fixed effects from a LMER model simillar to the models used here. https://lmudge13.github.io/sample_code/mixed_effects.html.
library(sjPlot)
mod <- lme4::lmer(log(elkhorn_LAI) ~ c.urchinden + c.fishmass +c.maxD +
(1|site), REML= FALSE, data= subset(me_data, LAI_nonzero ==1))
p <- sjPlot::plot_model(mod,
axis.labels=c("Urchin", "Depth", "Fish"),
show.values=TRUE, show.p=TRUE,
title="Effect of Herbivores on Coral Cover")
Which creates
I would like to be able to clip the x-axis so that its range of the graph is -1:2 and does not have the pieces extending past those points. I also want to add a central line at x = 0.
To clip the extent of the plot I've tried using both:
p + xlim (-1, 2)
and
p + scale_x_continuous(name="Estimates", limits=c(-1, 2))
Both of which return:
Scale for x is already present.
Adding another scale for x, which will replace the existing scale.
Error: Discrete value supplied to continuous scale
Too add a line at x = 0 I've tried using:
p + geom_vline(xintercept = 0)
Which doesn't change the plot.
I'm not sure if sjPlots
is the right package to do this with or if just using the ggplot
package would allow the plots to be more customizable.
site,spur,fish_biomass,urchin_count,urchin_den,elkhorn_LAI,PC_macro,PC_turf,PC_coral,PC_CTB,maxD,maxD_meter,depth_cat,LAI_nonzero,c.fishmass,c.urchinden,c.maxD
1,1,1994.34,4,0.1,8053.2,39,20,4,14,30,9.14,shallow,1,0.662672275,-0.296119996,-0.941602086
1,2,1523.535,30,0.75,36721,25.45454545,11.81818182,6.363636364,20,30,9.14,shallow,1,0.288763207,2.170396749,-0.941602086
1,3,413.46,30,0.75,53745,32,25,3,22,30,9.14,shallow,1,-0.592848307,2.170396749,-0.941602086
1,4,607.02,24,0.6,6689,31,14,10,19,30,9.14,shallow,1,-0.439124707,1.601200577,-0.941602086
1,5,1550.507,35,0.875,426000,12,23,34,12,30,9.14,shallow,1,0.310184125,2.644726892,-0.941602086
1,6,930.744,47,1.175,103648.5,25,19,12,21,30,9.14,shallow,1,-0.182026038,3.783119236,-0.941602086
2,1,1167.735,5,0.125,46784,20,34,10,22,40,12.19,deep,1,0.006190069,-0.201253968,0.160272696
2,2,637.623,3,0.075,7963.2,45,23,13,3,40,12.19,deep,1,-0.414820082,-0.390986025,0.160272696
2,3,3233.046,0,0,193259.25,18,29,22,14,40,12.19,deep,1,1.646441364,-0.675584111,0.160272696
2,5,687.512,1,0.025,19608.4,17,29,8,24,40,12.19,deep,1,-0.37519869,-0.580718082,0.160272696
2,6,1549.638,1,0.025,65270,31,23,15,17,40,12.19,deep,1,0.309493973,-0.580718082,0.160272696
3,1,300.429,17,0.425,13436.55,16,11,23,22,24,7.31,shallow,1,-0.682616503,0.937138376,-1.602726956
3,2,183.235,9,0.225,92235.5,23.63636364,7.272727273,20,22.72727273,24,7.31,shallow,1,-0.775690917,0.178210147,-1.602726956
3,3,435.729,13,0.325,0,26,18,20,24,24,7.31,shallow,0,-0.575162468,0.557674262,-1.602726956
3,4,379.274,12,0.3,9128.2,28,14,11,19,24,7.31,shallow,1,-0.619998518,0.462808233,-1.602726956
4,1,378.704,0,0,21368.6,41,19,13,10,35,10.67,shallow,1,-0.620451207,-0.675584111,-0.390664695
4,2,1055.283,0,0,3617.5,37,17,14,14,35,10.67,shallow,1,-0.083118292,-0.675584111,-0.390664695
4,3,1068.231,0,0,19650,44.54545455,20,20,4.545454545,35,10.67,shallow,1,-0.072835107,-0.675584111,-0.390664695
4,4,691.049,0,0,2189.6,54,14,6,9,35,10.67,shallow,1,-0.372389636,-0.675584111,-0.390664695
5,1,2059.932,0,0,0,30,13,14,15,45,13.72,deep,0,0.714764847,-0.675584111,0.711210087
5,2,832.302,0,0,0,43,20,9,11,45,13.72,deep,0,-0.260207783,-0.675584111,0.711210087
5,3,1927.144,1,0.025,7065.3,26,26,3,14,45,13.72,deep,1,0.60930582,-0.580718082,0.711210087
5,4,982.168,0,0,0,31,33,2,18,45,13.72,deep,0,-0.141185562,-0.675584111,0.711210087
6,1,513.111,0,0,0,47,19,10,4,53,16.15,deep,0,-0.513706385,-0.675584111,1.592709912
6,2,286.11,0,0,0,47,13,9,2,53,16.15,deep,0,-0.693988523,-0.675584111,1.592709912
6,3,593.168,0,0,0,62,10,3,3,53,16.15,deep,0,-0.45012584,-0.675584111,1.592709912
7,1,929.558,6,0.15,14234.05,41,23,15,3,32,9.75,shallow,1,-0.182967948,-0.106387939,-0.72122713
7,2,976.104,23,0.575,9897,30,37,2,15,32,9.75,shallow,1,-0.146001536,1.506334548,-0.72122713
7,3,1662.733,8,0.2,37344,22,33,24,10,32,9.75,shallow,1,0.399312998,0.083344118,-0.72122713
7,4,502.627,5,0.125,0,42,25,10,8,32,9.75,shallow,0,-0.522032683,-0.201253968,-0.72122713
8,1,1475.118,0,0,0,12,48,12,16,50,15.24,deep,0,0.250310864,-0.675584111,1.262147478
8,2,1939.011,0,0,0,21,33,7,20,50,15.24,deep,0,0.618730484,-0.675584111,1.262147478
8,3,2303.829,0,0,0,14,42,4,25,50,15.24,deep,0,0.908465636,-0.675584111,1.262147478
8,4,1168.356,0,0,0,20,57,7,3,50,15.24,deep,0,0.006683261,-0.675584111,1.262147478
9,1,643.765,1,0.025,0,36,35,3,8,44,13.41,deep,0,-0.409942161,-0.580718082,0.601022608
9,2,1708.074,2,0.05,36556,44,22,16,3,44,13.41,deep,1,0.43532241,-0.485852054,0.601022608
9,3,3380.41,0,0,23534.95,48,24,10,1,44,13.41,deep,1,1.763476518,-0.675584111,0.601022608
9,4,1034.796,2,0.05,160083.7,32.72727273,17.27272727,18.18181818,22.72727273,44,13.41,deep,1,-0.099388881,-0.485852054,0.601022608
9,5,1439.278,0,0,19200,43,28,10,7,44,13.41,deep,1,0.22184706,-0.675584111,0.601022608
9,6,288.153,2,0.05,0,30,24,10,12,44,13.41,deep,0,-0.692365991,-0.485852054,0.601022608
10,1,158.886,7,0.175,0,37,18,8,19,27,8.23,shallow,0,-0.795028672,-0.01152191,-1.272164521
10,2,231.219,2,0.05,0,45,15,10,16,27,8.23,shallow,0,-0.737582459,-0.485852054,-1.272164521
10,3,557.18,11,0.275,1700,42,15,10,10,27,8.23,shallow,1,-0.478707184,0.367942204,-1.272164521
10,4,562.854,15,0.375,153246.5,20,21,23,11,27,8.23,shallow,1,-0.474200944,0.747406319,-1.272164521
11,1,675.749,0,0,0,49.09090909,14.54545455,8.181818182,6.363636364,51,15.54,deep,0,-0.384540758,-0.675584111,1.372334956
11,2,926.084,0,0,0,38,21,16,7,51,15.54,deep,0,-0.185726967,-0.675584111,1.372334956
11,3,403.814,0,0,0,33,20,10,11,51,15.54,deep,0,-0.600509072,-0.675584111,1.372334956
11,4,504.364,0,0,0,33,19,16,14,51,15.54,deep,0,-0.520653173,-0.675584111,1.372334956
11,5,212.557,1,0.025,0,31,14,14,11,51,15.54,deep,0,-0.75240365,-0.580718082,1.372334956
12,1,269.588,15,0.375,17289.5,31,31,8,14,35,10.67,shallow,1,-0.707110146,0.747406319,-0.390664695
12,2,254.149,17,0.425,155412,24,26,15,20,35,10.67,shallow,1,-0.71937166,0.937138376,-0.390664695
12,3,399.036,16,0.4,79880.3,38,36,11,7,35,10.67,shallow,1,-0.604303717,0.842272348,-0.390664695
12,4,403.057,19,0.475,3722.4,17,42,6,10,35,10.67,shallow,1,-0.601110275,1.126870434,-0.390664695
You need to use ylim
and geom_hline
:
me_data <- read.table(text= readClipboard(),
header = T, stringsAsFactors = F, sep = ",")
library(sjPlot)
library(ggplot2)
library(lme4)
mod <- lmer(log(elkhorn_LAI) ~ c.urchinden + c.fishmass +c.maxD + (1|site),
REML= FALSE, data= subset(me_data, LAI_nonzero ==1))
p <- plot_model(mod,
axis.labels=c("Urchin", "Depth", "Fish"),
show.values=TRUE, show.p=TRUE,
title="Effect of Herbivores on Coral Cover")
p +
ylim(-0.5, 1.5) + # or + scale_y_continuous(limits = c(-0.5, 1.5))
geom_hline(yintercept = 0)
#> Scale for y is already present.
#> Adding another scale for y, which will replace the existing scale.
Created on 2024-02-27 with reprex v2.0.2