0

I am plotting a simple linear model with ggplot2. However, when I increase the size of the line, the ribbon does not change size (understandable). However, how would I scale the ribbon so that it matches the increase in line thickness?

Here is a simple example using the iris dataset:

library(ggplot2)

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
    geom_point() +
    stat_smooth(method = "lm", col = "red")

enter image description here

As you can see when you increase size (I'm over exaggerating the size here), then more of the ribbon is covered up.

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
    geom_point() +
    stat_smooth(method = "lm", col = "red", size = 5)

enter image description here

Essentially, the ribbon only needs to extend out as much as the additional thickness of the line that is displacing/obscuring the ribbon.

Expected Results

enter image description here

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
  • Not sure I understand; the shaded area represents the 95% confidence interval, https://stackoverflow.com/questions/29554796/meaning-of-band-width-in-ggplot-geom-smooth-lm. Are you interested in showing that band 5x as wide? – Jon Spring Aug 25 '22 at 00:06
  • @JonSpring Not 5x as large as that would make the ribbon a little too big. I added a little more of a description and image. Essentially, what I am trying to say is that if the line increases in thickness by say 1 cm, then the ribbon would need to expand by 0.5 cm on each side. – AndrewGB Aug 25 '22 at 00:23
  • Why do you want this? The confidence band doesn't change if you change the line thickness. That means you don't want the ribbon to represent the confidence band. But that leads to the question what the ribbon is supposed to represent. – Roland Aug 25 '22 at 05:40

1 Answers1

1

Perhaps like this?

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
  geom_point() +
  stat_smooth(method = "lm", col = "red", size = 5, 
              aes(ymin = after_stat(y - 5*se),
                  ymax = after_stat(y + 5*se)))

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • This seemed to be about the right thickness if I changed it to `2.5*se` (at least for size = 5). – AndrewGB Aug 25 '22 at 00:24
  • 1
    Hmm, so you want the `se` ribbon (based on a numeric calculation related to the data) to be adjusted based on the print width of the line? That's a little tricky since different print settings (e.g. different resolution) will change the line width. I hope an approximate manual solution suffices... – Jon Spring Aug 25 '22 at 03:55