Black Lives Matter. Please consider donating to Black Girls Code today.
https://www.blackgirlscode.com/

geom_point in ggplot2

How to make a scatter chart in ggplot2. Examples of scatter charts and line charts with fits and regressions.


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.

Scatter Chart

library(plotly)

set.seed(955)
# Make some noisily increasing data
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

p <- ggplot(dat, aes(x=xvar, y=yvar)) +
    geom_point(shape=1)      # Use hollow circles

fig <- ggplotly(p)

fig

Liner Regression w/ smooth

library(plotly)

set.seed(955)
# Make some noisily increasing data
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

p <- ggplot(dat, aes(x=xvar, y=yvar)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm)   # Add linear regression line


fig <- ggplotly(p)

fig

Without Shading

library(plotly)

set.seed(955)
# Make some noisily increasing data
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

p <- ggplot(dat, aes(x=xvar, y=yvar)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm,   # Add linear regression line
                se=FALSE)    # Don't add shaded confidence region

fig <- ggplotly(p)

fig

Loess Smoothed Fit

library(plotly)

set.seed(955)
# Make some noisily increasing data
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

p <- ggplot(dat, aes(x=xvar, y=yvar)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth()            # Add a loess smoothed fit curve with confidence region
# > geom_smooth: method="auto" and size of largest group is less than 1000, so using loess.
# Use 'method = x' to change the smoothing method.

fig <- ggplotly(p)

fig

Constrained Slope

library(plotly)

set.seed(1234)

n <- 20

x1 <- rnorm(n); x2 <- rnorm(n)
y1 <- 2 * x1 + rnorm(n)
y2 <- 3 * x2 + (2 + rnorm(n))
A <- as.factor(rep(c(1, 2), each = n))
df <- data.frame(x = c(x1, x2), y = c(y1, y2), A = A)
fm <- lm(y ~ x + A, data = df)

p <- ggplot(data = cbind(df, pred = predict(fm)), aes(x = x, y = y, color = A))
p <- p + geom_point() + geom_line(aes(y = pred))

fig <- ggplotly(p)

fig

Inspire by Stack Overflow

Stat Summary

library(plotly)

hist <- data.frame(date=Sys.Date() + 0:13, counts=1:14)
hist <- transform(hist, weekday=factor(weekdays(date), levels=c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')))

p <- ggplot(hist, aes(x=weekday, y=counts, group=1)) +
    geom_point(stat='summary', fun.y=sum) +
    stat_summary(fun.y=sum, geom="line")

fig <- ggplotly(p)

fig

Inspire by Stack Overflow

Control Line Order

library(plotly)

dat <- data.frame(x = sample(1:10), y = sample(1:10), order = sample(1:10))
p <- ggplot(dat[order(dat$order),], aes(x, y)) + geom_point() + geom_text(aes(y = y + 0.25,label =      order)) +
     geom_path()

fig <- ggplotly(p)

fig

Horizontal Line w/ Segment

library(plotly)

p <- ggplot(mtcars,aes(mpg,qsec))+geom_point() +
  geom_segment(aes(x=15,xend=20,y=18,yend=18))

fig <- ggplotly(p)

fig

Inspired by Stack Overflow

Add Points

library(plotly)

df <- data.frame(time=as.factor(c(1,1,2,2,3,3,4,4,5,5)), 
               value=as.numeric(c(7, 8, 9, 10, 10, 11, 10.5, 11.4, 10.9, 11.6)), 
               side=as.factor(c("E","F","E","F","E","F","E","F","E","F")))


p <- ggplot(df, aes(time, value, group=side, colour=side)) + 
     geom_line(size=1)
p <- p + geom_point()

fig <- ggplotly(p)

fig

Add Regression w/ Abline

library(plotly)

set.seed(1)
x <-  1:10
dd <- rbind(data.frame(x=x,fac="a", y=x+rnorm(10)),
            data.frame(x=2*x,fac="b", y=x+rnorm(10)))
coef <- lm(y~x:fac, data=dd)$coefficients
p <- qplot(data=dd, x=x, y=y, color=fac)+
    geom_abline(slope=coef["x:faca"], intercept=coef["(Intercept)"])+
    geom_abline(slope=coef["x:facb"], intercept=coef["(Intercept)"])

fig <- ggplotly(p)

fig

Inspired by Stats Exchange

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)