-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2021-08-04-continuous-error-bars.Rmd
138 lines (109 loc) · 3.03 KB
/
2021-08-04-continuous-error-bars.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
---
description: How to make Continuous Error Bands in ggplot2 with Plotly.
name: Continuous Error Bands
permalink: ggplot2/continuous-error-bars/
thumnail_github: continuous-error-bars.png
layout: base
language: ggplot2
display_as: statistical
page_type: u-guide
order: 15
output:
html_document:
keep_md: true
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
## 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`.
```{r}
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)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Add jitter
```{r}
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)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Create groups
To make sure groups do not overlay, use `position_dodge()`
```{r}
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.
```{r}
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)
```
<!--------------------- EXAMPLE BREAK ------------------------->