smooth in ggplot2

# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

x <- rnorm(100)
y <-  + .7*x + rnorm(100)
f1 <- as.factor(c(rep("A",50),rep("B",50)))
f2 <- as.factor(rep(c(rep("C",25),rep("D",25)),2))
df <- data.frame(cbind(x,y))
df$f1 <- f1
df$f2 <- f2

ggplot(df,aes(x=x,y=y)) +
    geom_point() +
    facet_grid(f1~f2) +
    stat_smooth(method="lm")

ggplotly()
Write feedback@plot.ly with questions or submit an issue. See the ggplot2 → plotly test tables for ggplot2 conversion coverage. This example was inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

p <- ggplot(mpg, aes(displ, hwy))
p + geom_point() + stat_smooth()

ggplotly()
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

myplot <- qplot(speed, dist, data=cars)
myplot + geom_smooth(method = "glm", formula = y~x, family = gaussian(link = 'log'))

ggplotly()
Inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

the.data <- read.table( header=TRUE, sep=",",
                        text="source,year,value
    S1,1976,56.98
    S1,1977,55.26
    S1,1978,68.83
    S1,1979,59.70
    S1,1980,57.58
    S1,1981,61.54
    S1,1982,48.65
    S1,1983,53.45
    S1,1984,45.95
    S1,1985,51.95
    S1,1986,51.85
    S1,1987,54.55
    S1,1988,51.61
    S1,1989,52.24
    S1,1990,49.28
    S1,1991,57.33
    S1,1992,51.28
    S1,1993,55.07
    S1,1994,50.88
    S2,1993,54.90
    S2,1994,51.20
    S2,1995,52.10
    S2,1996,51.40
    S3,2002,57.95
    S3,2003,47.95
    S3,2004,48.15
    S3,2005,37.80
    S3,2006,56.96
    S3,2007,48.91
    S3,2008,44.00
    S3,2009,45.35
    S3,2010,49.40
    S3,2011,51.19")
cutoff <- data.frame( x = c(-Inf, Inf), y = 50, cutoff = factor(50) )

ggplot(the.data, aes( year, value ) ) +
    geom_point(aes( colour = source )) +
    geom_smooth(aes( group = 1 )) +
    geom_hline(yintercept = 50)

ggplotly()
Inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

install.packages("foreign")
install.packages("truncreg")
install.packages("boot")
library(plotly)

dat <- read.dta("http://www.ats.ucla.edu/stat/data/truncreg.dta")

ggplot(dat, aes(x = langscore, y = achiv)) +
    geom_point() +
    stat_smooth(method = "loess") +
    facet_grid(. ~ prog, margins=TRUE)

ggplotly()
Inspired by the IDRE at UCLA.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plyr)
library(plotly)
install.packages("Lahman")
library(Lahman)

hr_stats_df <- ddply(Batting, .(playerID), function(df) c(mean(df$HR, na.rm = T),
                                                          max(df$HR, na.rm = T), sum(df$HR, na.rm = T), nrow(df)))
names(hr_stats_df)[c(2, 3, 4, 5)] <- c("HR.mean", "HR.max", "HR.total", "career.length")
hr_stats_long_df <- subset(hr_stats_df, career.length >= 10)
Batting_hr <- merge(Batting, hr_stats_long_df)
Batting_hr_cy <- ddply(Batting_hr, .(playerID), function(df) transform(df, career.year = yearID -
                                                                           min(yearID) + 1))
start_year_df <- ddply(Batting_hr_cy, .(playerID), function(df) min(df$yearID))
names(start_year_df)[2] <- "start.year"

# Merge this with other data.
Batting_hr_cy2 <- merge(Batting_hr_cy, start_year_df)
Batting_early <- subset(Batting_hr_cy2, start.year < 1940)
Batting_late <- subset(Batting_hr_cy2, start.year > 1950)
tot_HR_early <- subset(Batting_early, select = c(playerID, HR.total))

# Remove the duplicate rows:
tot_HR_early <- unique(tot_HR_early)
tot_HR_early_srt <- arrange(tot_HR_early, desc(HR.total))
top10_HR_hitters_early <- tot_HR_early_srt[1:10, "playerID"]
tot_HR_late <- subset(Batting_late, select = c(playerID, HR.total))

# Remove the duplicate rows:
tot_HR_late <- unique(tot_HR_late)
tot_HR_late_srt <- arrange(tot_HR_late, desc(HR.total))
top10_HR_hitters_late <- tot_HR_late_srt[1:10, "playerID"]
Batting_early_top10 <- subset(Batting_early, playerID %in% top10_HR_hitters_early)

ggplot(data = Batting_early_top10, aes(x = career.year, y = HR/AB)) + geom_point() +
    facet_wrap(~playerID, ncol = 3) + geom_smooth()

ggplotly()
Inspired by Steven Buechler.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

x <- 1:10
y <- jitter(x^2)

DF <- data.frame(x, y)

ggplot(DF, aes(x = x, y = y)) + geom_point() +
    stat_smooth(method = 'lm', aes(colour = 'linear'), se = FALSE) +
    stat_smooth(method = 'lm', formula = y ~ poly(x,2), aes(colour = 'polynomial'), se= FALSE) +
    stat_smooth(method = 'nls', formula = y ~ a * log(x) +b, aes(colour = 'logarithmic'), se = FALSE, start = list(a=1,b=1)) +
    stat_smooth(method = 'nls', formula = y ~ a*exp(b *x), aes(colour = 'Exponential'), se = FALSE, start = list(a=1,b=1))

ggplotly()
Inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

set.seed(1987)
pkgs <- c("ggplot2", "mgcv", "MASS")
invisible(lapply(pkgs, require, character.only = TRUE))

load(url('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.sav'))
titanic3 <- na.omit(titanic3[, -c(3,8:14)])
titanic3$class_sex <- apply(titanic3, 1,
                            function(x) paste(x[1], x[3], collapse = "_"))
titanic3$class_sex <- factor(titanic3$class_sex)
train <- titanic3[sample(row.names(titanic3),
                         size = round(nrow(titanic3) / 2)), ]
test <- titanic3[!(row.names(titanic3) %in% row.names(train)), ]

sim.data <- expand.grid(sex = c("male", "female"), sibsp = 0,
                        age = seq(1, 80), pclass = c("1st", "2nd", "3rd"))

glm.fit <- glm(survived ~ poly(age, 2) * sex * pclass + sibsp,
               "binomial", train)

inv.logit <- function(x) exp(x) / (1 + exp(x))
glm.pred <- predict(glm.fit, newdata = test, se.fit = TRUE)
pred <- data.frame(mean = inv.logit(glm.pred$fit),
                   lo = inv.logit(glm.pred$fit - 2 * glm.pred$se.fit),
                   hi = inv.logit(glm.pred$fit + 2 * glm.pred$se.fit),
                   survived = test$survived)
pred <- pred[order(pred$mean), ]
pred$id <- seq_along(pred$mean)
row.names(pred) <- NULL

pred <- predict(glm.fit, newdata = sim.data, se.fit = TRUE)
sim.data$mean <- inv.logit(pred$fit)
sim.data$lo <- inv.logit(pred$fit - 2 * pred$se.fit)
sim.data$hi <- inv.logit(pred$fit + 2 * pred$se.fit)

p <- ggplot(titanic3, aes(x = age, y = survived))
p <- p + geom_point()
p <- p + facet_grid(sex ~ pclass)
p <- p + geom_line(data = sim.data, aes(y = mean))
p <- p + geom_ribbon(data = sim.data, aes(y = mean, ymin = lo, ymax = hi),
                     alpha = .25)
p + labs(x = "Passenger Age", y = "Probability of Survival")

ggplotly()
Inspired by Zachary Jones.