-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2021-08-04-error-bars.Rmd
56 lines (46 loc) · 1.24 KB
/
2021-08-04-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
---
name: Error Bars
permalink: ggplot2/error-bars/
description: How to make Error Plots in ggplot2 with Plotly.
layout: base
thumnail_github: error-bars.png
language: ggplot2
page_type: u-guide
display_as: statistical
order: 1
output:
html_document:
keep_md: true
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
### Basic Error Bar
```{r}
library(plotly)
df <- data.frame(x = 1:10,
y = 1:10,
ymin = (1:10) - runif(10),
ymax = (1:10) + runif(10),
xmin = (1:10) - runif(10),
xmax = (1:10) + runif(10))
p <- ggplot(data = df,aes(x = x,y = y)) +
geom_point() +
geom_errorbar(aes(ymin = ymin,ymax = ymax)) +
geom_errorbarh(aes(xmin = xmin,xmax = xmax))
ggplotly(p)
```
### Margin Error Bar
```{r}
library(plotly)
population <- data.frame(Year=seq(1790, 1970, length.out=length(uspop)),
Population=uspop,
Error=rnorm(length(uspop), 5))
library(ggplot2)
p <- ggplot(population, aes(x=Year, y=Population,
ymin=Population-Error, ymax=Population+Error))+
geom_line()+
geom_point(pch=2)+
geom_errorbar(width=0.9)
ggplotly(p)
```