Q:

How to use a column for a shaded area in ggplot,R?

I have a dataframe of 2 columns, my values and the standard error.
What I want is to plot a line with my values and then add the standard error as a shaded area around my line. 
I do not even know how to start as most of the examples use the geom_ribbon and not an existing column. 
I am really confused, is it even possible to plot a shaded area based on an existing column? (any suggestions of tutorials or demos are welcomed!) 

A:

you need 3 columns, timestamp, value and std. And it's as simple as use your std column inside the aes(ymin, ymax):
library(tidyverse)
huron <- data.frame(year = 1875:1972, 
                value = LakeHuron,
                std = runif(length(LakeHuron),0,1))

huron %>% 
  ggplot(aes(year, value)) + 
  geom_ribbon(aes(ymin = value - std, ymax = value + std), fill = "steelblue2") + 
  geom_line(color = "firebrick", size = 1)

In case you want to group your data, you should use fill = your_group and group = your_group inside the aes:
library(tidyverse)
huron <- data.frame(year = rep(1875:1972,2), 
                group = c(rep("a",98),rep("b",98)),
                value = c(LakeHuron, LakeHuron + 5),
                std = runif(length(LakeHuron)*2,0,1))

huron %>% 
  ggplot(aes(year, value, fill = group, group=group)) + 
  geom_ribbon(aes(ymin = value - std, ymax = value + std), fill = 
  "steelblue2") + 
  geom_line(color = "firebrick", size = 1)

I posted this tip here: https://typethepipe.com/vizs-and-tips/ggplot_geom_ribbon_shadow/ for more info. Hope it helps!

