3

my ggplot R-code works perfectly ok with my other datasets but I'm stumbled with why it's not working for one particular data set. See image below where the filled confidence interval stops at 0.10: enter image description here

For reproducing the problem:

library(nlme)
library(ggeffects)
library(ggplot2)
SurfaceCoverage <- c(0.02,0.04,0.06,0.08,0.1,0.12,0.02,0.04,0.06,0.08,0.1,0.12)
SpecificSurfaceEnergy <- c(18.0052997,15.9636971,14.2951057,13.0263081,13.0816591,13.3825573,2.9267577,2.2889628,1.8909175,1.0083036,0.5683574,0.1681063)
sample <- c(1,1,1,1,1,1,2,2,2,2,2,2)

highW <- data.frame(sample,SurfaceCoverage,SpecificSurfaceEnergy)

highW$sample <- sub("^", "Wettable", highW$sample)
highW$RelativeHumidity <- "High relative humidity"; highW$group <- "Wettable"
highW$sR <- paste(highW$sample,highW$RelativeHumidity)

dfhighW <- data.frame(
  "y"=c(highW$SpecificSurfaceEnergy),
  "x"=c(highW$SurfaceCoverage),
  "b"=c(highW$sample),
  "sR"=c(highW$sR)
)

mixed.lme <- lme(y~log(x),random=~1|b,data=dfhighW)
pred.mmhighW <- ggpredict(mixed.lme, terms = c("x"))

(ggplot(pred.mmhighW) + 
    geom_line(aes(x = x, y = predicted)) +          # slope
    geom_ribbon(aes(x = x, ymin = predicted - std.error, ymax = predicted + std.error), 
                fill = "lightgrey", alpha = 0.5) +  # error band
    geom_point(data = dfhighW,                      # adding the raw data (scaled values)
               aes(x = x, y = y, shape = b)) + 
    xlim(0.01,0.2) + 
    ylim(0,30) +
    labs(title = "") + 
    ylab(bquote('Specific Surface Energy ' (mJ/m^2))) +
    xlab(bquote('Surface Coverage ' (n/n[m]) )) +
    theme_minimal()
)

Can someone advise me how to fix this? Thanks.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Catalyst
  • 426
  • 3
  • 12

2 Answers2

3

The last part of your ribbon has disappeared because you have excluded it from the plot. The lower edge of your ribbon is the following vector:

pred.mmhighW$predicted - pred.mmhighW$std.error
#> [1]  3.91264018  2.37386628  1.47061258  0.82834206  0.32935718 -0.07886245

Note the final value is a small negative number, but you have set the y axis limits with:

ylim(0, 30)

So anything negative will be cut off. If you change to

ylim(-2, 30)

You get

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thanks @Allan Cameron!!!!! Omg I keep making such simple mistakes. You save my day. Also I probably need to rest to have a fresh eye to pick this up. Thank you again! – Catalyst Feb 19 '22 at 23:42
1

I don't know whether this is already answered previously, but coord_cartesian and scales::squish are two solutions to this problem.

  • coord_cartesian adjusts the viewport without adjusting the spacing of grid lines etc. (unlike xlim()/scale_*_continuous(limits = ...), which will "zoom")
  • scales::squish() is suboptimal if you are "squishing" lines and points, not just edgeless polygons (in the case of fill/polygons, squishing and clipping produce the same results)
gg0 <- (ggplot(pred.mmhighW) 
    + geom_ribbon(aes(x = x, ymin = predicted - std.error,
                      ymax = predicted + std.error), 
                  fill = "lightgrey", alpha = 0.5)
    + theme_minimal()
)

## set lower limit to 5 for a more obvious effect
gg0 + coord_cartesian(ylim = c(5, 30))
gg0 + scale_y_continuous(limits = c(5, 30),
                         ## oob = "out of bounds" behaviour
                         oob = scales::squish)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • this is almost the same as https://stackoverflow.com/questions/57000066/clip-only-1-axis-using-coord-cartesian/57002219#57002219 – Ben Bolker Feb 20 '22 at 00:32