Reputation: 1508
I have some data over hours that looks like this:
df <- data.frame('hour' = rep(0:23, 2),
'mean' = rnorm(48),
'sd' = rnorm(48))
I wish to plot geom_smooth()
over the entire period:
df %>%
ggplot(aes(y = mean, x = hour)) +
geom_smooth(aes(y = sd))
However, by default my hour
parameter is being factored such that every hour only appears once, as can be seen if one adds geom_point()
, but I want all 48 (many more in reality) values to appear on my x axis.
I realize there must be a single argument I'm missing but can't find.
Upvotes: 1
Views: 344
Reputation: 1271
If I understand correctly, you want the line of geom_smooth to represent the mean column in your data frame, and the error ribbon to represent the sd column.
I believe this should give the output you're looking for:
library(dplyr)
library(ggplot2)
df %>%
ggplot(aes(y = mean, x = hour)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "loess", se = FALSE) +
stat_summary(aes(y = sd), fun.data = mean_se, geom="ribbon", alpha=0.25)
Upvotes: 0
Reputation: 4283
You need to define the hour you use as a date_time object, where the hours in the second halve are on the next day
You may use the following code:
library(dplyr)
library(ggplot2)
set.seed(9876) # for reproducibility
df <- data.frame('hour' = seq(c(ISOdate(2019,12,1)), by = "hour", length.out = 48),
'mean' = rnorm(48),
'sd' = rnorm(48))
df %>%
ggplot(aes(y = mean, x = hour)) +
geom_smooth(aes(y = sd))
Please, let me know whether this is what you want.
Upvotes: 1