library(ggplot2)

model <- lm(mpg ~ wt + factor(cyl), data=mtcars)

x <- coef(model)
x
##  (Intercept)           wt factor(cyl)6 factor(cyl)8 
##    33.990794    -3.205613    -4.255582    -6.070860
intercept <- signif(x[1], 3)
terms <- paste(signif(x[-1], 3), names(x[-1]), sep="*", collapse= " + ")
terms
## [1] "-3.21*wt + -4.26*factor(cyl)6 + -6.07*factor(cyl)8"
e1 <- paste(intercept, terms, collapse = " + ")
e2 <- paste("R^2 = ", round(summary(model)$r.squared, 3))

#annotateの使い方
ggplot(mtcars, aes(x=wt, y=mpg)) + 
    geom_point() + 
    geom_smooth(method=lm) +
    annotate("text", label=e1, x=max(mtcars$wt), y=max(mtcars$mpg), 
             hjust=1, size=3, vjust=0) +
    annotate("text", label=e2, x=max(mtcars$wt), y=max(mtcars$mpg), 
             hjust=1, size=3, vjust=2)

library(ggplot2)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)

# GET EQUATION AND R-SQUARED AS STRING
# SOURCE: http://goo.gl/K4yh

lm_eqn = function(df){
    m = lm(y ~ x, df);
    eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
         list(a = format(coef(m)[1], digits = 2), 
              b = format(coef(m)[2], digits = 2), 
             r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq));                 
}

p <- ggplot(data = df, aes(x = x, y = y)) +
            geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
            geom_point()

p <- p + geom_text(aes(x = 25, y = 300, label = lm_eqn(df)), parse = TRUE)
print(p)

library(ggplot2)
library(reshape2)

d <- data.frame("lower"=rep(26.65933,6),"upper"=rep(28.83431,6),"median"=rep(27.54782,6),"grid"=rep(-4.845770,6),"trueX"=rep(3.998449,6),"trueY"=rep(27.73697,6),"xDens"=rep(0.02803692,6))
d
##      lower    upper   median     grid    trueX    trueY      xDens
## 1 26.65933 28.83431 27.54782 -4.84577 3.998449 27.73697 0.02803692
## 2 26.65933 28.83431 27.54782 -4.84577 3.998449 27.73697 0.02803692
## 3 26.65933 28.83431 27.54782 -4.84577 3.998449 27.73697 0.02803692
## 4 26.65933 28.83431 27.54782 -4.84577 3.998449 27.73697 0.02803692
## 5 26.65933 28.83431 27.54782 -4.84577 3.998449 27.73697 0.02803692
## 6 26.65933 28.83431 27.54782 -4.84577 3.998449 27.73697 0.02803692
d2 <- melt(subset(d, select = -c(trueX)), id = "grid")
head(d2,5)
##       grid variable    value
## 1 -4.84577    lower 26.65933
## 2 -4.84577    lower 26.65933
## 3 -4.84577    lower 26.65933
## 4 -4.84577    lower 26.65933
## 5 -4.84577    lower 26.65933
##???
ggplot(d2, aes(grid, value, colour = variable, linetype = variable, size = variable)) +
  geom_line() +
  scale_colour_manual("", values = c("red", "red", "black", "blue", "black"), breaks = levels(d2$variable)) +
  scale_linetype_manual("", values = c(2, 2, 1, 1, 1), breaks = levels(d2$variable)) +
  scale_size_manual("", values = c(0.5, 0.5, 0.5, 0.5, 0.25), breaks = levels(d2$variable))

library(ggplot2)

# Create a long data.frame to store data...
growth_series = data.frame ("read_day" = c(0, 3, 9, 0, 3, 9, 0, 2, 8), 
"series_id" = c("p1s1", "p1s1", "p1s1", "p1s2", "p1s2", "p1s2", "p3s4", "p3s4", "p3s4"),
"mean_od" = c(0.6, 0.9, 1.3, 0.3, 0.6, 1.0, 0.2, 0.5, 1.2),
"sd_od" = c(0.1, 0.2, 0.2, 0.1, 0.1, 0.3, 0.04, 0.1, 0.3),
"n_in_stat" = c(8, 8, 8, 8, 7, 5, 8, 7, 2)
)
growth_series
##   read_day series_id mean_od sd_od n_in_stat
## 1        0      p1s1     0.6  0.10         8
## 2        3      p1s1     0.9  0.20         8
## 3        9      p1s1     1.3  0.20         8
## 4        0      p1s2     0.3  0.10         8
## 5        3      p1s2     0.6  0.10         7
## 6        9      p1s2     1.0  0.30         5
## 7        0      p3s4     0.2  0.04         8
## 8        2      p3s4     0.5  0.10         7
## 9        8      p3s4     1.2  0.30         2
# Now gives us some example long form data...
#growth_series 

growth_series$series_id <- factor(
     growth_series$series_id, 
     labels=c('treatment 1', 't2', 't3'))
     
     
ggplot(data = growth_series, aes(x = read_day, y = mean_od, group = series_id, color = series_id)) +
geom_line(size = 1.5) +
geom_point(aes(size = n_in_stat)) +
geom_errorbar(aes(ymin = mean_od - sd_od, ymax = mean_od + sd_od), size = 1, width = 0.3) +
xlab("Days") + ylab("Log (O.D. 730 nm)") +
#scale_y_log2() +
scale_colour_hue('my legend', 
   breaks = levels(growth_series$series_id), 
   labels=c('t1', 't2', 't3'))

```

library(ggplot2)

grp <- gl(n=4,k=20,labels=c("group a","group b","group c", "group d"))
value <- runif(n=80, min=10, max=150)
outcome <- cut(value,2)
data <- data.frame(grp,value,outcome)
  
ggplot(data, aes(grp, fill=outcome)) + geom_bar() +xlab("group") +
  ylab("number of subjects") + 
  scale_fill_discrete("Serologic response", 
                      breaks=c("(10.1,79.9]","(79.9,150]"), 
                      labels=c("double negative", "positive for a and/or b"))  

ggplot(data, aes(grp, fill=outcome)) + geom_bar() +
  scale_x_discrete("group") +
  scale_y_continuous("number of subjects") + 
  scale_fill_discrete("Serologic response", 
                      breaks=c("(10.1,79.9]","(79.9,150]"), 
                      labels=c("double negative", "positive for a and/or b"))                      

library(ggplot2)

# Create a long data.frame to store data...
growth_series = data.frame ("read_day" = c(0, 3, 9, 0, 3, 9, 0, 2, 8), 
"series_id" = c("p1s1", "p1s1", "p1s1", "p1s2", "p1s2", "p1s2", "p3s4", "p3s4", "p3s4"),
"mean_od" = c(0.6, 0.9, 1.3, 0.3, 0.6, 1.0, 0.2, 0.5, 1.2),
"sd_od" = c(0.1, 0.2, 0.2, 0.1, 0.1, 0.3, 0.04, 0.1, 0.3),
"n_in_stat" = c(8, 8, 8, 8, 7, 5, 8, 7, 2)
)

growth_series
##   read_day series_id mean_od sd_od n_in_stat
## 1        0      p1s1     0.6  0.10         8
## 2        3      p1s1     0.9  0.20         8
## 3        9      p1s1     1.3  0.20         8
## 4        0      p1s2     0.3  0.10         8
## 5        3      p1s2     0.6  0.10         7
## 6        9      p1s2     1.0  0.30         5
## 7        0      p3s4     0.2  0.04         8
## 8        2      p3s4     0.5  0.10         7
## 9        8      p3s4     1.2  0.30         2
# Plot using ggplot...
ggplot(data = growth_series, aes(x = read_day, y = mean_od, group = series_id, color = series_id)) +
geom_line(size = 1.5) +
geom_point(aes(size =n_in_stat>4)) + scale_size_manual(values=c(2,5)) +
geom_errorbar(aes(ymin = mean_od - sd_od, ymax = mean_od + sd_od), size = 1, width = 0.3) +
xlab("Days") + ylab("Log (O.D. 730 nm)") +
#scale_y_log2() +
scale_colour_hue('my legend', breaks = levels(growth_series$series_id), labels=c('t1','t2', 't3'))

ff <- function(x){ifelse(x < 4, 5, 2)}

# Plot using ggplot...
ggplot(data = growth_series, aes(x = read_day, y = mean_od, group = series_id, color = series_id)) +
geom_line(size = 1.5) +
  
geom_point(aes(size = n_in_stat)) +
  
#scale_size_manual(values=c(2,5)) +
geom_errorbar(aes(ymin = mean_od - sd_od, ymax = mean_od + sd_od), size = 1, width = 0.3) +
xlab("Days") + ylab("Log (O.D. 730 nm)") +
#scale_y_log2() +
scale_colour_hue('my legend', breaks = levels(growth_series$series_id), labels=c('t1','t2', 't3'))

# Plot using ggplot...
ggplot(data = growth_series, aes(x = read_day, y = mean_od, group = series_id, color = series_id)) +
geom_line(size = 1.5) +
  
geom_point(aes(size = ff(n_in_stat)))  +
  
#scale_size_manual(values=c(2,5)) +
geom_errorbar(aes(ymin = mean_od - sd_od, ymax = mean_od + sd_od), size = 1, width = 0.3) +
xlab("Days") + ylab("Log (O.D. 730 nm)") +
#scale_y_log2() +
scale_colour_hue('my legend', breaks = levels(growth_series$series_id), labels=c('t1','t2', 't3'))