Skip to content

Instantly share code, notes, and snippets.

@infotroph
Last active November 22, 2015 22:50
Show Gist options
  • Save infotroph/13464b9b56d8020889a1 to your computer and use it in GitHub Desktop.
Save infotroph/13464b9b56d8020889a1 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(dplyr)
library(zoo) # for rollmean()
d_mean = (diamonds
%>% mutate(price=round(price, -2))
%>% group_by(price)
%>% mutate(mean_x = mean(x)))
d_roll = (diamonds
%>% arrange(price)
%>% mutate(rollmean_x = rollmean(
x=x,
k=floor(nrow(diamonds)/10),
align="center",
fill=NA)))
(ggplot(diamonds, aes(x=price, y=x))
+geom_point()
+geom_point(aes(y=mean_x), data=d_mean, color="red")
+geom_point(aes(y=rollmean_x), data=d_roll, color="goldenrod")
+geom_smooth(method="loess", span=0.1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment