0

I want to include a jump in my plot. Thy y-axis should start from 0 but the there should be a break which goes to 1500 and from there in 100 unit increments up to 2000. This works in my code, but the space between 0 and 1500 is still expanded, and i cannot figure out how to remove that:enter image description here

I tried using that code with the package ggplot2 and sjPlot: kcalPlot+ylab("Energy Intake (kcal)")+ scale_y_continuous(limits = c(0,2000),expand=c(0,0),breaks = c(0,1500,seq(1600,2000, by=100)), labels = c("0", "1500", "1600", "1700", "1800", "1900","2000")) which produces me the plot above, but it is still expanded. kcalPlot is produced by this kcalPlot <- plot_model(kcal3, type="slope"), where kcal3 is a model from lmer(). Is it possible to get rid of that space, or is there another way to it? Data Here would be a snippet of my data, it is a dropbox link, i hope this is okay. To produce the lme-model i used: kcal3 <- lmer(EnergyIntake~Group+Timepoint+BMI+(1+Group|ID), data=kcalL, REML=F) and then the code above to produce the plot with sjplot. Thanks for your help!

JanSch
  • 3
  • 3

3 Answers3

1

We could hack this. (I prefer not to load data from random links from strangers, so I just used standard data to show two approaches.) Compare:

ggplot(mtcars, aes(wt, mpg*20 + 1300)) +
  geom_point() +
  scale_y_continuous(limits = c(0,2000),expand=c(0,0),
                     breaks = c(0,1500,seq(1600,2000, by=100)),     
                     labels = c("0", "1500", "1600", "1700", "1800", "1900","2000"))

enter image description here

ggplot(mtcars, aes(wt, mpg*20 + 1300)) +
  geom_point() +
  geom_hline(yintercept = 1450, color = "white", linewidth = 3) +
  scale_y_continuous(limits = c(1400,2000),expand=c(0,0),
                     breaks = c(1400,1500,seq(1600,2000, by=100)),     
                     labels = c("0", "1500", "1600", "1700", "1800", "1900","2000"))

enter image description here

Or we could use patchwork, which might be necessary if the smooth extends beyond the top plot range:

p1 <- ggplot(mtcars, aes(wt, mpg*20 + 1300)) +
  geom_point() +
  geom_smooth(method = "lm") +
  scale_y_continuous(breaks = scales::breaks_width(100))

library(patchwork)
(p1 + coord_cartesian(ylim = c(1500, 2000)) +
    labs(x = NULL) + theme(axis.ticks.x = element_blank(),
                           axis.text.x = element_blank())) /
(p1 + coord_cartesian(ylim = c(0, 200))) +
plot_layout(heights = c(600, 200))

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

It is not possible with ggplot what you intend (AFAIK). See here it is explained why and which alternatives you have Using ggplot2, can I insert a break in the axis?

Here I provide a solution suggested by @Hadley Wickham's using facet_zoom from ggforce package (also on the linked page):

Note I changed your data, because in your provided data there are only Controls so that not contrast was possible, and I could only provide the first 300 rows due to space problems:

library(lme4)
library(sjPlot)
kcal3 <- lmer(EnergyIntake~Group+Timepoint+BMI+(1+Group|ID), data=kcalL, REML=F) 


library(ggforce)
my_func <- function(x) {
  kcalPlot1[[x]]+
  ylab("Energy Intake (kcal)")+
  theme_minimal()+
  facet_zoom(ylim = c(0, 1000))
}
  
library(ggpubr)
ggarrange(my_func(1), my_func(2), my_func(3), ncol = 1)

enter image description here

data:

kcalL <- structure(list(ID = c("21", "23", "16", "17", "18", "19", "20", 
"22", "9", "25", "26", "27", "28", "29", "21", "23", "16", "17", 
"18", "19", "20", "22", "9", "25", "26", "27", "28", "29", "21", 
"23", "16", "17", "18", "19", "20", "22", "9", "25", "26", "27", 
"28", "29", "21", "23", "16", "17", "18", "19", "20", "22", "9", 
"25", "26", "27", "28", "29", "21", "23", "16", "17", "18", "19", 
"20", "22", "9", "25", "26", "27", "28", "29", "21", "23", "16", 
"17", "18", "19", "20", "22", "9", "25", "26", "27", "28", "29", 
"21", "23", "16", "17", "18", "19", "20", "22", "9", "25", "26", 
"27", "28", "29", "21", "23", "16", "17", "18", "19", "20", "22", 
"9", "25", "26", "27", "28", "29", "21", "23", "16", "17", "18", 
"19", "20", "22", "9", "25", "26", "27", "28", "29", "21", "23", 
"16", "17", "18", "19", "20", "22", "9", "25", "26", "27", "28", 
"29", "21", "23", "16", "17", "18", "19", "20", "22", "9", "25", 
"26", "27", "28", "29", "21", "23", "16", "17", "18", "19", "20", 
"22", "9", "25", "26", "27", "28", "29", "21", "23", "16", "17", 
"18", "19", "20", "22", "9", "25", "26", "27", "28", "29", "21", 
"23", "16", "17", "18", "19", "20", "22", "9", "25", "26", "27", 
"28", "29", "21", "23", "16", "17", "18", "19", "20", "22", "9", 
"25", "26", "27", "28", "29", "21", "23", "16", "17", "18", "19", 
"20", "22", "9", "25", "26", "27", "28", "29", "21", "23", "16", 
"17", "18", "19", "20", "22", "9", "25", "26", "27", "28", "29", 
"21", "23", "16", "17", "18", "19", "20", "22", "9", "25", "26", 
"27", "28", "29", "21", "23", "16", "17", "18", "19", "20", "22", 
"9", "25", "26", "27", "28", "29", "21", "23", "16", "17", "18", 
"19", "20", "22", "9", "25", "26", "27", "28", "29", "21", "23", 
"16", "17", "18", "19", "20", "22", "9", "25", "26", "27", "28", 
"29", "21", "23", "16", "17", "18", "19"), Group = c("Control", 
"Treatment", "Control", "Control", "Control", "Treatment", "Treatment", 
"Treatment", "Control", "Treatment", "Control", "Treatment", 
"Treatment", "Control", "Treatment", "Control", "Treatment", 
"Treatment", "Treatment", "Control", "Control", "Treatment", 
"Control", "Treatment", "Control", "Control", "Control", "Treatment", 
"Treatment", "Treatment", "Control", "Control", "Control", "Treatment", 
"Treatment", "Treatment", "Control", "Treatment", "Control", 
"Treatment", "Treatment", "Treatment", "Control", "Treatment", 
"Control", "Control", "Treatment", "Control", "Control", "Control", 
"Treatment", "Treatment", "Control", "Treatment", "Treatment", 
"Treatment", "Treatment", "Treatment", "Treatment", "Control", 
"Control", "Control", "Treatment", "Control", "Control", "Treatment", 
"Treatment", "Treatment", "Treatment", "Control", "Treatment", 
"Control", "Control", "Control", "Control", "Control", "Treatment", 
"Treatment", "Treatment", "Treatment", "Control", "Control", 
"Treatment", "Control", "Treatment", "Treatment", "Treatment", 
"Treatment", "Control", "Control", "Treatment", "Treatment", 
"Control", "Treatment", "Control", "Treatment", "Control", "Treatment", 
"Treatment", "Treatment", "Control", "Treatment", "Treatment", 
"Treatment", "Treatment", "Treatment", "Control", "Treatment", 
"Control", "Treatment", "Control", "Control", "Control", "Control", 
"Treatment", "Control", "Control", "Treatment", "Control", "Control", 
"Control", "Treatment", "Treatment", "Treatment", "Control", 
"Treatment", "Treatment", "Control", "Treatment", "Treatment", 
"Control", "Control", "Treatment", "Treatment", "Control", "Control", 
"Treatment", "Treatment", "Treatment", "Control", "Treatment", 
"Control", "Control", "Control", "Control", "Treatment", "Control", 
"Treatment", "Treatment", "Control", "Treatment", "Treatment", 
"Treatment", "Treatment", "Control", "Treatment", "Control", 
"Control", "Treatment", "Treatment", "Control", "Treatment", 
"Treatment", "Treatment", "Treatment", "Treatment", "Control", 
"Treatment", "Treatment", "Control", "Treatment", "Control", 
"Treatment", "Control", "Control", "Control", "Treatment", "Control", 
"Control", "Treatment", "Control", "Control", "Treatment", "Treatment", 
"Treatment", "Control", "Treatment", "Treatment", "Control", 
"Control", "Treatment", "Control", "Control", "Treatment", "Treatment", 
"Treatment", "Treatment", "Control", "Treatment", "Control", 
"Treatment", "Control", "Control", "Control", "Treatment", "Control", 
"Control", "Control", "Control", "Treatment", "Control", "Control", 
"Treatment", "Treatment", "Control", "Treatment", "Control", 
"Control", "Treatment", "Control", "Control", "Control", "Treatment", 
"Control", "Control", "Treatment", "Control", "Control", "Control", 
"Treatment", "Treatment", "Treatment", "Control", "Treatment", 
"Control", "Treatment", "Control", "Control", "Control", "Control", 
"Treatment", "Control", "Treatment", "Control", "Treatment", 
"Treatment", "Control", "Control", "Treatment", "Treatment", 
"Treatment", "Control", "Treatment", "Control", "Treatment", 
"Treatment", "Control", "Control", "Control", "Control", "Control", 
"Treatment", "Control", "Treatment", "Treatment", "Treatment", 
"Control", "Treatment", "Treatment", "Control", "Treatment", 
"Treatment", "Treatment", "Treatment", "Treatment", "Control", 
"Control", "Control", "Treatment", "Control", "Treatment", "Control", 
"Control", "Control", "Control", "Control", "Control", "Control", 
"Treatment", "Treatment", "Control", "Control", "Treatment", 
"Control", "Control", "Treatment", "Treatment", "Control", "Control", 
"Treatment"), BMI = c(29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109, 28.3782254413762, 27.2443225118122, 21.015625, 
20.7624716553288, 21.8101320946436, 21.0586803677664, 35.0162050185439, 
40.1225701426793, 29.3827824061632, 23.7966468361276, 26.6346593507087, 
20.8558680367766, 23.5716214652212, 28.7317468902109, 28.3782254413762, 
27.2443225118122, 21.015625, 20.7624716553288, 21.8101320946436, 
21.0586803677664, 35.0162050185439, 40.1225701426793, 29.3827824061632, 
23.7966468361276, 26.6346593507087, 20.8558680367766, 23.5716214652212, 
28.7317468902109), Timepoint = c("Day1", "Day1", "Day1", "Day1", 
"Day1", "Day1", "Day1", "Day1", "Day1", "Day1", "Day1", "Day1", 
"Day1", "Day1", "Day2", "Day2", "Day2", "Day2", "Day2", "Day2", 
"Day2", "Day2", "Day2", "Day2", "Day2", "Day2", "Day2", "Day2", 
"Day3", "Day3", "Day3", "Day3", "Day3", "Day3", "Day3", "Day3", 
"Day3", "Day3", "Day3", "Day3", "Day3", "Day3", "Day4", "Day4", 
"Day4", "Day4", "Day4", "Day4", "Day4", "Day4", "Day4", "Day4", 
"Day4", "Day4", "Day4", "Day4", "Day5", "Day5", "Day5", "Day5", 
"Day5", "Day5", "Day5", "Day5", "Day5", "Day5", "Day5", "Day5", 
"Day5", "Day5", "Day6", "Day6", "Day6", "Day6", "Day6", "Day6", 
"Day6", "Day6", "Day6", "Day6", "Day6", "Day6", "Day6", "Day6", 
"Day7", "Day7", "Day7", "Day7", "Day7", "Day7", "Day7", "Day7", 
"Day7", "Day7", "Day7", "Day7", "Day7", "Day7", "Day8", "Day8", 
"Day8", "Day8", "Day8", "Day8", "Day8", "Day8", "Day8", "Day8", 
"Day8", "Day8", "Day8", "Day8", "Day9", "Day9", "Day9", "Day9", 
"Day9", "Day9", "Day9", "Day9", "Day9", "Day9", "Day9", "Day9", 
"Day9", "Day9", "Day10", "Day10", "Day10", "Day10", "Day10", 
"Day10", "Day10", "Day10", "Day10", "Day10", "Day10", "Day10", 
"Day10", "Day10", "Day11", "Day11", "Day11", "Day11", "Day11", 
"Day11", "Day11", "Day11", "Day11", "Day11", "Day11", "Day11", 
"Day11", "Day11", "Day12", "Day12", "Day12", "Day12", "Day12", 
"Day12", "Day12", "Day12", "Day12", "Day12", "Day12", "Day12", 
"Day12", "Day12", "Day13", "Day13", "Day13", "Day13", "Day13", 
"Day13", "Day13", "Day13", "Day13", "Day13", "Day13", "Day13", 
"Day13", "Day13", "Day14", "Day14", "Day14", "Day14", "Day14", 
"Day14", "Day14", "Day14", "Day14", "Day14", "Day14", "Day14", 
"Day14", "Day14", "Day15", "Day15", "Day15", "Day15", "Day15", 
"Day15", "Day15", "Day15", "Day15", "Day15", "Day15", "Day15", 
"Day15", "Day15", "Day16", "Day16", "Day16", "Day16", "Day16", 
"Day16", "Day16", "Day16", "Day16", "Day16", "Day16", "Day16", 
"Day16", "Day16", "Day17", "Day17", "Day17", "Day17", "Day17", 
"Day17", "Day17", "Day17", "Day17", "Day17", "Day17", "Day17", 
"Day17", "Day17", "Day18", "Day18", "Day18", "Day18", "Day18", 
"Day18", "Day18", "Day18", "Day18", "Day18", "Day18", "Day18", 
"Day18", "Day18", "Day19", "Day19", "Day19", "Day19", "Day19", 
"Day19", "Day19", "Day19", "Day19", "Day19", "Day19", "Day19", 
"Day19", "Day19", "Day20", "Day20", "Day20", "Day20", "Day20", 
"Day20", "Day20", "Day20", "Day20", "Day20", "Day20", "Day20", 
"Day20", "Day20", "Day21", "Day21", "Day21", "Day21", "Day21", 
"Day21", "Day21", "Day21", "Day21", "Day21", "Day21", "Day21", 
"Day21", "Day21", "Day22", "Day22", "Day22", "Day22", "Day22", 
"Day22"), EnergyIntake = c(3813, 2229, 2010, 1744, 1756, 1782, 
2946, 1460, 2596, 1037, 1787, 1754, 1835, 2105, 2579, 2470, 1608, 
1882, 1988, 2273, 2943, 1617, 2509, 1291, 1766, 1232, 1524, 2073, 
3239, 3083, 1608, 1755, 1787, 1791, 2926, 1557, 2906, 2001, 1794, 
1237, 1659, 1677, 1976, 2412, 2010, 1991, 2399, 2326, 2922, 1479, 
2505, 1414, 1950, 1678, 1382, 2378, 3607, 2368, 1273, 1723, 1811, 
2311, 2937, 1429, 2658, 1327, 2304, 1547, 1812, 2857, 1850, 2489, 
1842, 1776, 1303, 1658, 2952, 1465, 2460, 1824, 1000, 1221, 1357, 
2404, 3486, 1965, 1359, 1475, 1615, 1796, 2950, 1483, 1956, 1263, 
1293, 1088, 1771, 1753, 3568, 2045, 1451, 1584, 1430, 2304, 2858, 
1856, 2802, 1377, 969, 1482, 1432, 2019, 4295, 2321, 1672, 1949, 
1778, 1806, 2961, 1443, 2585, 1171, 1296, 1528, 1192, 1705, 2963, 
2595, 1619, 2333, 2410, 1773, 2364, 1314, 2211, 1238, 1178, 1451, 
1369, 1507, 2953, 2876, 1416, 1813, 1741, 2281, 2942, 1441, 1829, 
1382, 1792, 1844, 1428, 2907, 3303, 2688, 1279, 1158, 1563, 1769, 
2978, 1275, 2483, 1448, 677, 1581, 1710, 1750, 2904, 2409, 1505, 
1656, 1791, 2289, 2374, 963, 2153, 1910, 2213, 1840, 1347, 1645, 
3378, 2067, 1622, 1207, 1732, 1809, 2944, 1140, 2496, 1158, 1655, 
1286, 1795, 1996, 3027, 2449, 1374, 1520, 1632, 1898, 2366, 1253, 
2520, 1259, 1408, 1702, 1865, 1738, 2936, 2314, 1568, 2123, 2303, 
1613, 2359, 1354, 1933, 1191, 1406, 1488, 2113, 2041, 3664, 1860, 
1225, 1798, 2018, 1503, 2350, 1133, 2466, 1669, 1264, 1353, 1930, 
1532, 3901, 2348, 1100, 2029, 1489, 1844, 2970, 1176, 2558, 1537, 
1078, 1594, 1434, 1946, 3630, 2259, 1597, 1749, 1734, 1684, 2978, 
1055, 2205, 1336, 1169, 1676, 1155, 2719, 3725, 2222, 1791, 1218, 
1578, 1936, 2934, 1066, 2897, 1261, 1234, 1522, 1587, 1312, 2811, 
2656, 1730, 1762, 1646, 1624, 2961, 1094, 2096, 1217, 1029, 1662, 
1115, 2281, 3457, 1859, 1463, 1822, 2094, 1671)), row.names = c("2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", 
"26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", 
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", 
"48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", 
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", 
"81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", 
"92", "93", "94", "95", "96", "97", "98", "99", "100", "101", 
"102", "103", "104", "105", "106", "107", "108", "109", "110", 
"111", "112", "113", "114", "115", "116", "117", "118", "119", 
"120", "121", "122", "123", "124", "125", "126", "127", "128", 
"129", "130", "131", "132", "133", "134", "135", "136", "137", 
"138", "139", "140", "141", "142", "143", "144", "145", "146", 
"147", "148", "149", "150", "151", "152", "153", "154", "155", 
"156", "157", "158", "159", "160", "161", "162", "163", "164", 
"165", "166", "167", "168", "169", "170", "171", "172", "173", 
"174", "175", "176", "177", "178", "179", "180", "181", "182", 
"183", "184", "185", "186", "187", "188", "189", "190", "191", 
"192", "193", "194", "195", "196", "197", "198", "199", "200", 
"201", "202", "203", "204", "205", "206", "207", "208", "209", 
"210", "211", "212", "213", "214", "215", "216", "217", "218", 
"219", "220", "221", "222", "223", "224", "225", "226", "227", 
"228", "229", "230", "231", "232", "233", "234", "235", "236", 
"237", "238", "239", "240", "241", "242", "243", "244", "245", 
"246", "247", "248", "249", "250", "251", "252", "253", "254", 
"255", "256", "257", "258", "259", "260", "261", "262", "263", 
"264", "265", "266", "267", "268", "269", "270", "271", "272", 
"273", "274", "275", "276", "277", "278", "279", "280", "281", 
"282", "283", "284", "285", "286", "287", "288", "289", "290", 
"291", "292", "293", "294", "295", "296", "297", "298", "299", 
"300", "301"), class = "data.frame")
TarJae
  • 72,363
  • 6
  • 19
  • 66
0

Here's an alternative hack, assuming OP already has the model (created using lmer() from the lme4 package) and plot (created using plot_model() from the sjPlot package), and doesn't want to undo the work done there.

Simulate the model / plot creation process, based off the example used in lmer's help file:

library(lme4)
library(sjPlot)

data(Orthodont,package="nlme")

kcal3 <- lmer(distance ~ age + Sex + (age|Subject), data=Orthodont)
kcalPlot <- plot_model(kcal3, type = "slope")

Hack:

library(ggplot2)
library(dplyr)

# replace the facet_wrap in the original with facet_grid for ease of alignment 
# layer; impact should be minimal since the desire is to have a common y-axis.
p <- kcalPlot + 
  facet_grid(cols = vars(group), scales = "free_x")

# duplicate plot but remove x-axis elements
# if you want to specify y-axis breaks for your actual plot, can add them
# here as well (other than the y = 0 break, which is addressed below)
p.top <- p + 
  # scale_y_continuous(breaks = seq(1500, 2000, by = 100)) +
  theme(axis.ticks.length.x.bottom = unit(0, "pt"), 
        axis.text.x = element_blank(), 
        axis.title.x = element_blank(), 
        axis.ticks.x = element_blank(), 
        plot.margin = unit(c(5.5, 5.5, 0, 5.5), "points"))

# duplicate plot again for its basic structure, for y = 0 range, with a new 
# text layer added to provide the plot break symbol for the leftmost facet
p.bottom <- p +
  geom_text(data = get_model_data(kcal3, type = "slope") %>%
              group_by(group) %>%
              summarise(x = -Inf, y = 0.5, .groups = "drop") %>%
              arrange(group) %>%
              mutate(alpha = c(1, rep(0, n()-1))),
            aes(label = "\\\\", alpha = alpha), angle = 90, size = 5) +
  scale_y_continuous(breaks = 0) +
  scale_alpha_identity() +
  coord_cartesian(ylim = c(0, 1), clip = "off") +
  theme(axis.title.y = element_blank(),
        plot.margin = unit(c(0, 5.5, 5.5, 5.5), "points"),
        panel.grid.minor.y = element_blank(), 
        plot.background = element_blank(),
        strip.background = element_blank(), 
        strip.text = element_blank())

# stitch the two plots together & adjust the relative amount of space for the
# y=0 portion (I set it as 20% of the top plot's height)
cowplot::plot_grid(p.top, p.bottom, align = "v", axis = "lr", ncol = 1,
                   rel_heights = c(1, 0.2))

Result:

result

Z.Lin
  • 28,055
  • 6
  • 54
  • 94