Strip Charts in ggplot2

How to make Strip Charts in ggplot2 with Plotly.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

Basic stripchart

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_jitter(position=position_jitter(0.2))
p <- p + coord_flip()

ggplotly(p)

Change point size

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
        geom_jitter(position=position_jitter(0.2), cex=1.2)

ggplotly(p)

Change shape

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
        geom_jitter(position=position_jitter(0.2), shape=17)

ggplotly(p)

Add summary statistics

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_jitter(position=position_jitter(0.2), cex=1.2)
p <- p + stat_summary(fun.y=mean, geom="point", shape=18,
                 size=3, color="red")

ggplotly(p)

To add standard deviation use mean_sdl function which computes the mean plus or minus a constant times the standard deviation.

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
    geom_jitter(position=position_jitter(0.2))
p <- p + stat_summary(fun.data=mean_sdl, mult=1,
                 geom="pointrange", color="red")

ggplotly(p)

You can change the representation of the statistics by changing geom, for example setting it to crossbar.

Add box plot

To add a box plot you can use geom_boxplot(). Likewise, you can add a notched boxplot with geom_boxplot(notch = TRUE) and a violin plot with geom_violin(trim = FALSE).

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
        geom_boxplot()+
        geom_jitter(position=position_jitter(0.2))

ggplotly(p)

Colour data by groups

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) +
  geom_jitter(position=position_jitter(0.2))

ggplotly(p)
library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, shape=dose)) +
  geom_jitter(position=position_jitter(0.2))+
  labs(title="Plot of length  by dose",x="Dose (mg)", y = "Length")
p <- p + theme_classic()

ggplotly(p)

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)