stat_smooth in ggplot2

Add a smoothed line in ggplot2 and R with stat_smooth.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Basic

library(plotly)

p <- ggplot(mpg, aes(displ, hwy))
p <- p + geom_point() + stat_smooth()

fig <- ggplotly(p)

fig

Inspired by ggplot2 documentation

Trend Lines

library(plotly)

x <- 1:10
y <- jitter(x^2)

DF <- data.frame(x, y)

p <- ggplot(DF, aes(x = x, y = y)) + geom_point() +
    stat_smooth(method = 'lm', aes(colour = 'linear'), se = FALSE) +
    stat_smooth(method = 'lm', formula = y ~ poly(x,2), aes(colour = 'polynomial'), se= FALSE) +
    stat_smooth(method = 'nls', formula = y ~ a * log(x) +b, aes(colour = 'logarithmic'), se = FALSE, start = list(a=1,b=1)) +
    stat_smooth(method = 'nls', formula = y ~ a*exp(b *x), aes(colour = 'Exponential'), se = FALSE, start = list(a=1,b=1))

fig <- ggplotly(p)

fig

Inspired by Stack Oveflow

Facetgrid

library(plotly)

x <- rnorm(100)
y <-  + .7*x + rnorm(100)
f1 <- as.factor(c(rep("A",50),rep("B",50)))
f2 <- as.factor(rep(c(rep("C",25),rep("D",25)),2))
df <- data.frame(cbind(x,y))
df$f1 <- f1
df$f2 <- f2

p <- ggplot(df,aes(x=x,y=y)) +
    geom_point() +
    facet_grid(f1~f2) +
    stat_smooth(method="lm")

fig <- ggplotly(p)

fig

Inspired by Stack Overflow

Add Legend

library(plotly)
set.seed(123)

x <- rnorm(1000)
y1 <- 2*x + rnorm(1000)
y2 <- x^2 + rnorm(1000)

ds <- data.frame(data = x, 
                 Linear = y1, 
                 Quadratic = y2)


cols1 <- c("#ff8080", "#66b3ff")
cols2 <- c("#ff4d4d", "#3399ff")

p <- ggplot(ds, aes(x = data)) + 
  geom_point(aes(y = Linear, color = "Linear"), size = 2, alpha = 0.5) + 
  geom_point(aes(y = Quadratic, color = "Non Linear"), size = 2, alpha = 0.5) + 
  stat_smooth(aes(x = data, y = Linear, linetype = "Linear Fit"), method = "lm", formula = y ~ x, se = F, size = 0.25, color = cols2[1]) + 
  stat_smooth(aes(x = data, y = Quadratic, linetype = "Quadratic Fit"), method = "lm", formula = y ~ poly(x,2), se = F, size = 0.25, color = cols2[2]) + 
  scale_color_manual(name = "Relationship", values = c(cols1[1], cols1[2])) + 
  scale_linetype_manual(name = "Fit Type", values = c(2, 2)) + 
  ggtitle("Manual Legend for Stat Smooth")

fig <- ggplotly(p)

fig

Inspired by Stack Overflow

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)