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

# geom_abline in ggplot2

How to use the abline geom in ggplot2 to add a line with specified slope and intercept to the plot.

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.

### Line

add line for mean using geom_vline

library(plotly)

set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)),
rating = c(rnorm(200),rnorm(200, mean=.8)))

p <- ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="black", fill="white") +
geom_vline(aes(xintercept=mean(rating, na.rm=T)),   # Ignore NA values for mean
color="red", linetype="dashed", size=1)

fig <- ggplotly(p)

fig


### Histogram

overlaid histograms with geom_vline

library(plotly)
library(plyr)
cdat <- ddply(dat, "cond", summarise, rating.mean=mean(rating))

# Overlaid histograms with means
p <- ggplot(dat, aes(x=rating, fill=cond)) +
geom_histogram(binwidth=.5, alpha=.5, position="identity") +
geom_vline(data=cdat, aes(xintercept=rating.mean),
linetype="dashed", size=1)

fig <- ggplotly(p)

fig


### Histogram Means

histograms with geom_vline means

library(plotly)
library(plyr)
cdat <- ddply(dat, "cond", summarise, rating.mean=mean(rating))

# With mean lines
p <- ggplot(dat, aes(x=rating)) + geom_histogram(binwidth=.5, colour="black", fill="white") +
facet_grid(cond ~ .) +
geom_vline(data=cdat, aes(xintercept=rating.mean),
linetype="dashed", size=1, colour="red")

fig <- ggplotly(p)

fig


### Density Plots

density plots with geom_vline means

library(plotly)
library(plyr)
cdat <- ddply(dat, "cond", summarise, rating.mean=mean(rating))

# Density plots with means
p <- ggplot(dat, aes(x=rating, colour=cond)) +
geom_density() +
geom_vline(data=cdat, aes(xintercept=rating.mean),
linetype="dashed", size=1)

fig <- ggplotly(p)

fig


### Horizontal Line

add horizontal line with geom_hline

library(plotly)

cond xval yval
control 11.5 10.8
control  9.3 12.9
control  8.0  9.9
control 11.5 10.1
control  8.6  8.3
control  9.9  9.5
control  8.8  8.7
control 11.7 10.1
control  9.7  9.3
control  9.8 12.0
treatment 10.4 10.6
treatment 12.1  8.6
treatment 11.2 11.0
treatment 10.0  8.8
treatment 12.9  9.5
treatment  9.1 10.0
treatment 13.4  9.6
treatment 11.6  9.8
treatment 11.5  9.8
treatment 12.0 10.6
')

# The basic scatterplot
p <- ggplot(dat, aes(x=xval, y=yval, colour=cond)) +
geom_point()

p <- p + geom_hline(aes(yintercept=10))

fig <- ggplotly(p)

fig


### Mean Line

add mean line with geom_hline

library(plotly)

cond xval yval
control 11.5 10.8
control  9.3 12.9
control  8.0  9.9
control 11.5 10.1
control  8.6  8.3
control  9.9  9.5
control  8.8  8.7
control 11.7 10.1
control  9.7  9.3
control  9.8 12.0
treatment 10.4 10.6
treatment 12.1  8.6
treatment 11.2 11.0
treatment 10.0  8.8
treatment 12.9  9.5
treatment  9.1 10.0
treatment 13.4  9.6
treatment 11.6  9.8
treatment 11.5  9.8
treatment 12.0 10.6
')

# The basic scatterplot
p <- ggplot(dat, aes(x=xval, y=yval, colour=cond)) +
geom_point()

mean1 <- mean(dat[dat$cond == "control", "xval"]) mean2 <- mean(dat[dat$cond == "treatment", "xval"])

# Add colored lines for the mean xval of each group
p <- p +
geom_vline(aes(xintercept=mean1), colour="green") +
geom_vline(aes(xintercept=mean2), colour="lightblue")

fig <- ggplotly(p)

fig


### Geomvline & Geomhline

use geomvline with geomhline

library(plotly)

cond xval yval
control 11.5 10.8
control  9.3 12.9
control  8.0  9.9
control 11.5 10.1
control  8.6  8.3
control  9.9  9.5
control  8.8  8.7
control 11.7 10.1
control  9.7  9.3
control  9.8 12.0
treatment 10.4 10.6
treatment 12.1  8.6
treatment 11.2 11.0
treatment 10.0  8.8
treatment 12.9  9.5
treatment  9.1 10.0
treatment 13.4  9.6
treatment 11.6  9.8
treatment 11.5  9.8
treatment 12.0 10.6
')

# The basic scatterplot
p <- ggplot(dat, aes(x=xval, y=yval, colour=cond)) + geom_point()

# Add a red dashed vertical line
p <- p + geom_hline(aes(yintercept=10)) +
geom_vline(aes(xintercept=11.5), colour="#BB0000", linetype="dashed")

fig <- ggplotly(p)

fig


These ggplot2 examples were inspired by the Cookbook for R.

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