Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29920db

Browse files
author
yankev
committed
updated error bars example
1 parent 9a643c5 commit 29920db

File tree

3 files changed

+52
-46
lines changed

3 files changed

+52
-46
lines changed

_posts/r/2015-07-30-error-bars.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

_posts/r/2015-07-30-error-bars.Rmd renamed to _posts/r/2016-02-22-error-bars.Rmd

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ library(plotly)
2828
p <- ggplot2::mpg %>% group_by(class) %>%
2929
summarise(mn = mean(hwy), sd = 1.96 * sd(hwy)) %>%
3030
arrange(desc(mn)) %>%
31-
plot_ly(x = class, y = mn, error_y = list(value = sd),
31+
plot_ly(x = class, y = mn, error_y = list(array = sd),
3232
mode = "markers", name = "Highway") %>%
3333
layout(yaxis = list(title = "Miles Per Gallon"))
3434
p
3535
3636
df2 <- mpg %>% group_by(class) %>%
37-
summarise(mn = mean(cty), sd = 1.96 * sd(cty))
37+
summarise(mn = mean(cty))
3838
39-
p2 <- add_trace(p, y = mn, error_y = list(value = sd),
39+
p2 <- add_trace(p, y = mn, error_y = list(value=10),
4040
name = "City", data = df2)
41+
#by default the `type` for error is `percent`, which takes
42+
#a percentage of the y value as the error bar.
4143
p2
4244
```

_posts/r/2016-02-22-error-bars.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Error Bars in R | Examples | Plotly
3+
name: Error Bars
4+
permalink: r/error-bars/
5+
description: How to add error bars to scatter plots in R.
6+
layout: base
7+
thumbnail: thumbnail/error-bar.jpg
8+
language: r
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: chart_type
12+
order: 11
13+
---
14+
15+
16+
# Error Bars
17+
18+
19+
```r
20+
library(dplyr)
21+
library(plotly)
22+
23+
p <- ggplot2::mpg %>% group_by(class) %>%
24+
summarise(mn = mean(hwy), sd = 1.96 * sd(hwy)) %>%
25+
arrange(desc(mn)) %>%
26+
plot_ly(x = class, y = mn, error_y = list(array = sd),
27+
mode = "markers", name = "Highway") %>%
28+
layout(yaxis = list(title = "Miles Per Gallon"))
29+
p
30+
```
31+
32+
<!--html_preserve--><div id="htmlwidget-4568" style="width:504px;height:504px;" class="plotly"></div>
33+
<script type="application/json" data-for="htmlwidget-4568">{"x":{"data":[{"type":"scatter","inherit":false,"x":["compact","subcompact","midsize","2seater","minivan","suv","pickup"],"y":[28.2978723404255,28.1428571428571,27.2926829268293,24.8,22.3636363636364,18.1290322580645,16.8787878787879],"error_y":{"array":[7.41197461022174,10.5350239397813,4.18642195442454,2.55552734283944,4.04280370759969,5.83682722794172,4.45758835463684]},"mode":"markers","name":"Highway"}],"layout":{"yaxis":{"title":"Miles Per Gallon"},"xaxis":{"title":"class"},"hovermode":"closest","margin":{"b":40,"l":60,"t":25,"r":10}},"url":null,"width":null,"height":null,"base_url":"https://plot.ly"},"evals":[]}</script><!--/html_preserve-->
34+
35+
```r
36+
df2 <- mpg %>% group_by(class) %>%
37+
summarise(mn = mean(cty))
38+
39+
p2 <- add_trace(p, y = mn, error_y = list(value=10),
40+
name = "City", data = df2)
41+
#by default the `type` for error is `percent`, which takes
42+
#a percentage of the y value as the error bar.
43+
p2
44+
```
45+
46+
<!--html_preserve--><div id="htmlwidget-9470" style="width:504px;height:504px;" class="plotly"></div>
47+
<script type="application/json" data-for="htmlwidget-9470">{"x":{"data":[{"type":"scatter","inherit":false,"x":["compact","subcompact","midsize","2seater","minivan","suv","pickup"],"y":[28.2978723404255,28.1428571428571,27.2926829268293,24.8,22.3636363636364,18.1290322580645,16.8787878787879],"error_y":{"array":[7.41197461022174,10.5350239397813,4.18642195442454,2.55552734283944,4.04280370759969,5.83682722794172,4.45758835463684]},"mode":"markers","name":"Highway"},{"y":[15.4,20.1276595744681,18.7560975609756,15.8181818181818,13,20.3714285714286,13.5],"error_y":{"value":10},"name":"City"}],"layout":{"yaxis":{"title":"Miles Per Gallon"},"xaxis":{"title":"class"},"hovermode":"closest","margin":{"b":40,"l":60,"t":25,"r":10}},"url":null,"width":null,"height":null,"base_url":"https://plot.ly"},"evals":[]}</script><!--/html_preserve-->

0 commit comments

Comments
 (0)