Black Lives Matter. Please consider donating to Black Girls Code today.

geom_quantile in ggplot2

How to use geom_quantile with Plotly.


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 Example

While common linear regression is a method of estimating the conditional mean of variable y based on the values of variable(s) x, quantile regression is a method that can give the conditional median (50th percentile) as well as any other quantile.

This dataset gives the effect of the mother's weight on her baby's birth weight, further divided according to whether the mother smokes or not. The line shows the median birth weight conditional on these two other variables.

library(plotly)
library(MASS)
library(quantreg)

df <- MASS::birthwt

df <- with(df, { #Make sure variables properly show up as categories
  race <- factor(race, labels = c("white", "black", "other"))
  ptd <- factor(ptl > 0)
  ftv <- factor(ftv)
  levels(ftv)[-(1:2)] <- "2+"
  data.frame(low = factor(low), age, lwt, race, smoke = (smoke > 0),
             ptd, ht = (ht > 0), ui = (ui > 0), ftv, bwt)
})

p <- ggplot(df, aes(lwt, bwt, colour = smoke)) +
  geom_point(size = 1) +
  geom_quantile(quantiles = 0.5)

fig <- ggplotly(p)

fig

With Quantiles

geom_quantile is capable of showing more than just the conditional median: here we show the median, the 10th percentile, and 90th percentiles as well. We see that, among nonsmokers, the likelihood of underweight babies decreases significantly as the mother's weight increases, but that mothers of all weights are roughly equally likely to give birth to the heaviest babies. Conversely, among smoking mothers, the likelihood of underweight babies seem to increase as mother's weight increases.

Given the small sample size for this dataset, it's wise not to draw too many conclusions; this is meant to illustrate the purpose of quantile regression. You can also adjust the lines' appearance.

library(plotly)
library(MASS)
library(dplyr)

df <- MASS::birthwt

df <- with(df, {
  race <- factor(race, labels = c("white", "black", "other"))
  ptd <- factor(ptl > 0)
  ftv <- factor(ftv)
  levels(ftv)[-(1:2)] <- "2+"
  data.frame(low = factor(low), age, lwt, race, smoke = (smoke > 0),
             ptd, ht = (ht > 0), ui = (ui > 0), ftv, bwt)
})

p <- ggplot(df, aes(lwt, bwt, colour=smoke)) +
  geom_point(size = 1) +
  geom_quantile(quantiles = c(0.1, 0.5, 0.9), size = 2, aes(alpha = ..quantile..)) +
  scale_alpha(range = c(0.3, 0.7))
fig <- ggplotly(p)

fig

Reference: ggplot2 docs

Reference

See https://plotly.com/r/reference for more information and options!

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)