简体   繁体   中英

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") :

在此处输入图片说明

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:

在此处输入图片说明

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)

ggplotly frequently gives unexpected results. If the end product you want is a plotly graph, then it's usually best to go straight to the plotly API. Using the plotly API also gives access to a much wider range of options than ggplotly.

Unfortunately, however, plotly does not come with the convenient built in calculation of statistics that geom_smooth provides. So we begin by calculating our fit and error range using lm() and predict.lm()

lm1 = lm(Proficiency~Demographic, data=df)
lm.df = data.frame(Demographic = sort(df$Demographic), 
                   Proficiency = predict(lm1)[order(df$Demographic)],
                   se = predict(lm1, se.fit = T)$se.fit[order(df$Demographic)])
lm.df$ymax = lm.df$Proficiency + lm.df$se
lm.df$ymin = lm.df$Proficiency - lm.df$se

Now we are ready to plot, using the plotly API directly

plot_ly() %>%
  add_trace(data=df, x=~Demographic, y=~Proficiency, type="scatter", mode="markers",
            size=~N.Tested, color=~N.Tested, colors = c("#8B1A1A", "#FFA500"), showlegend=F) %>%
  add_ribbons(data=lm.df, x=~Demographic, ymin = ~ymin, ymax = ~ymax,
              line = list(color="transparent"), showlegend=F,
              fillcolor = "#77777777") %>%
  add_trace(data=lm.df, x=~Demographic, y=~Proficiency, 
          type="scatter", mode="lines", line=list(color="black"), showlegend=F) 

在此处输入图片说明

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM