Tim Bedeaux Tim Bedeaux - 2 months ago 16
R Question

Remove border lines on geom_smooth confidence interval using ggplotly

I'm building a pretty intensive plot, but I am having a basic problem with geom_smooth that I can't seem to find any answers for. Here's how the plot looks now with

geom_smooth(method = "lm")
:

enter image description here

When I build the geom_smooth() portion of my plot, I'd like to change the line color. When I do this, it draws new lines bordering my confidence interval,

Calling
geom_smooth(method = "lm", color = "black")
returns this:

enter image description here

Is there a simple way to get rid of the border lines, but keep the main line black?

EDIT: I can't provide full code with data, but have provided circumstances that will reproduce the error here. You will need no more than this to answer the question. Per the comments below, its likely an interaction with plotly (ggplotly).

library(ggplot2)
library(plotly)
df <- data.frame(
Demographic = rnorm(1:100),
Proficiency = rnorm(1:100),
N.Tested = rnorm(1:100)
)

a <- ggplot(data = df,
aes(x = Demographic,
y = Proficiency)
)
b <- a + geom_point(aes(
text = "to be replaced",
color = N.Tested,
size = N.Tested
),
show.legend = FALSE)
c <- b + scale_color_gradient2(low = "firebrick4", high = "gold", mid = "orange", midpoint=300)
d <- c + geom_smooth(method = "lm", color="black")

ggplotly(d)

Answer

You could add just the regression line via geom_smooth and add the ribbon via geom_ribbon using stat = "smooth". It adds an extra step but separating the layers allows you to color the line without messing up the confidence ribbon.

d <- c + geom_smooth(method = "lm", se = FALSE, color = "black")
e <- d + geom_ribbon(stat = "smooth", method = "lm", alpha = .15)
ggplotly(e)

enter image description here