Continuous Error Bands in ggplot2
How to make Continuous Error Bands in ggplot2 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.
Default error bar plot
To create continuous errorbar plot we need to use df.summary
.
To add lower and upper error bars, use ymin = len-sd
and ymax = len+sd
.
library(plotly)
library(ggplot2)
library(dplyr)
df <- ToothGrowth
df$dose <- as.factor(df$dose)
df.summary <- df %>%
group_by(dose) %>%
summarise(
sd = sd(len, na.rm = TRUE),
len = mean(len)
)
p <- ggplot(df.summary, aes(dose, len)) +
geom_line(aes(group = 1)) +
geom_errorbar( aes(ymin = len-sd, ymax = len+sd),width = 0.2) +
geom_point(size = 2)
ggplotly(p)
Add jitter
library(plotly)
library(ggplot2)
library(dplyr)
df <- ToothGrowth
df$dose <- as.factor(df$dose)
df.summary <- df %>%
group_by(dose) %>%
summarise(
sd = sd(len, na.rm = TRUE),
len = mean(len)
)
p <- ggplot(df, aes(dose, len)) +
geom_jitter( position = position_jitter(0.2), color = "darkgray") +
geom_line(aes(group = 1), data = df.summary) +
geom_errorbar(
aes(ymin = len-sd, ymax = len+sd),
data = df.summary, width = 0.2) +
geom_point(data = df.summary, size = 2)
ggplotly(p)
Create groups
To make sure groups do not overlay, use position_dodge()
library(plotly)
library(ggplot2)
library(dplyr)
df <- ToothGrowth
df$dose <- as.factor(df$dose)
df.summary <- df %>%
group_by(dose, supp) %>%
summarise(
sd = sd(len),
len = mean(len)
)
p <- ggplot(df.summary, aes(dose, len)) +
geom_errorbar(
aes(ymin = len-sd, ymax = len+sd, color = supp),
position = position_dodge(0.3), width = 0.2
)+
geom_point(aes(color = supp), position = position_dodge(0.3)) +
scale_color_manual(values = c("#00AFBB", "#E7B800"))
ggplotly(p)
Add line with geom_line()
, remember to apply position_dodge()
to make sure groups do not overlay each other.
library(plotly)
library(ggplot2)
library(dplyr)
df <- ToothGrowth
df$dose <- as.factor(df$dose)
df.summary <- df %>%
group_by(dose, supp) %>%
summarise(
sd = sd(len),
len = mean(len)
)
p <- ggplot(df.summary, aes(dose, len)) +
geom_line(aes(linetype = supp, group = supp), position = position_dodge(0.3))+
geom_errorbar(
aes(ymin = len-sd, ymax = len+sd, color = supp),
position = position_dodge(0.3), width = 0.2
)+
geom_point(aes(color = supp), position = position_dodge(0.3)) +
scale_color_manual(values = c("#00AFBB", "#E7B800"))
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)