qplot

library(ggplot2)
## 
## Attaching package: 'ggplot2'
## 
## The following object is masked _by_ '.GlobalEnv':
## 
##     presidential
library("ggthemes")
library(gridExtra)
## Loading required package: grid
set.seed(1410) # Make the sample reproducible
dsmall <- diamonds[sample(nrow(diamonds), 100), ]

p1 <- qplot(carat, price, data = diamonds)
p2 <- qplot(log(carat), log(price), data = diamonds)
p3 <- qplot(carat, x * y * z, data = diamonds)
grid.arrange(p1,p2,p3,ncol=2)

p1 <- qplot(carat, price, data = dsmall, colour = color)
p2 <- qplot(carat, price, data = dsmall, shape = cut)
grid.arrange(p1,p2,ncol=1)

p1 <- qplot(carat, price, data = diamonds, alpha = I(1/10))
p2 <- qplot(carat, price, data = diamonds, alpha = I(1/100))
p3 <- qplot(carat, price, data = diamonds, alpha = I(1/200))
grid.arrange(p1,p2,p3,ncol=2)

p1 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth")) 
p2 <- qplot(carat, price, data = diamonds, geom = c("point", "smooth")) 
p3 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth"), 
  span = 0.2)+ggtitle("span = 0.2")
p4 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth"), 
  span = 1) + ggtitle("span = 1") 
grid.arrange(p1,p2,p3,p4,ncol=2)
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

p1 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth"), 
  method = "gam", formula = y ~ s(x))
p2 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth"), 
  method = "gam", formula = y ~ s(x, bs = "cs"))
grid.arrange(p1,p2,ncol=1)

library(splines)
p1 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth"), 
  method = "lm")
p2 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth"), 
  method = "lm", formula = y ~ ns(x, 5))
grid.arrange(p1,p2,ncol=1)

p1 <- qplot(color, price / carat, data = diamonds, geom = "jitter") + ggtitle("jitter")
p2 <- qplot(color, price / carat, data = diamonds, geom = "boxplot") + ggtitle("boxplot")

p3 <- qplot(color, price / carat, data = diamonds, geom = "jitter",
 alpha = I(1 / 5)) + ggtitle("alpha = I(1/5)")
p4 <- qplot(color, price / carat, data = diamonds, geom = "jitter",
 alpha = I(1 / 50)) + ggtitle("alpha = I(1/50)")
p5 <- qplot(color, price / carat, data = diamonds, geom = "jitter",
 alpha = I(1 / 200)) + ggtitle("alpha = I(1/200)")
grid.arrange(p1,p2,p3,p4,p5,ncol=2)

p1 <- qplot(carat, data = diamonds, geom = "histogram", binwidth = 1, 
  xlim = c(0,3))+ ggtitle("binwidth = 1")
p2 <- qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.1,
  xlim = c(0,3)) + ggtitle("binwidth =0.1")
p3 <- qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.01,
  xlim = c(0,3)) + ggtitle("binwidth = 0.01")
grid.arrange(p1,p2,p3,ncol=2)

p1 <- qplot(color, data = diamonds, geom = "bar")
p2 <- qplot(color, data = diamonds, geom = "bar", weight = carat) +
  scale_y_continuous("carat") + ggtitle("weight = carat")
grid.arrange(p1,p2,ncol=1)

year <- function(x) as.POSIXlt(x)$year + 1900

p1 <- qplot(unemploy / pop, uempmed, data = economics, 
   geom = c("point", "path")) +
  theme_hc() 
  
p2 <- qplot(unemploy / pop, uempmed, data = economics, 
  geom = "path", colour = year(date)) +
  theme_hc() 
  
grid.arrange(p1,p2,ncol=1)

p1 <- qplot(carat, data = diamonds, facets = color ~ ., 
  geom = "histogram", binwidth = 0.1, xlim = c(0, 3)) + ggtitle("count")

p2 <- qplot(carat, ..density.., data = diamonds, facets = color ~ .,
  geom = "histogram", binwidth = 0.1, xlim = c(0, 3)) + ggtitle("density")
grid.arrange(p1,p2,ncol=1)

p1 <- qplot(
  carat, price, data = dsmall, 
  xlab = "Weight (carats)", ylab = "Price ($)",
  main = "Price-weight relationship"
)
p2 <- qplot(
   carat, price/carat, data = dsmall, 
   ylab = expression(frac(price,carat)), 
   xlab = "Weight (carats)",  
   main = "Small diamonds", 
   xlim = c(.2,1)
)
p3 <- qplot(carat, price, data = dsmall, log = "xy") + ggtitle(" log = xy")
grid.arrange(p1,p2,p3,ncol=2)