Adding a smoother to a plot
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
qplot(x = carat, y = price, data = diamonds, geom = c("point", "smooth"), alpha = I(1/5))
## 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.
qplot(x = carat, y = price, data = diamonds, geom = c("point", "smooth"), alpha = I(1/5), se = FALSE)
## 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.
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "loess")
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "loess", span = 0.2, se = FALSE)
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "loess", span = 1, se = FALSE)
library(mgcv)
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "gam", formula = y ~ s(x))
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "gam", formula = y ~ s(x, bs = "cs"))
library(splines)
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "lm")
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "lm", formula = y ~ poly(x, 3))
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "lm", formula = y ~ ns(x, 4))
library(MASS)
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "rlm")
Histogram and Density Plots
qplot(x = carat, data = diamonds, geom = "histogram")
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
qplot(x = carat, data = diamonds, geom = "histogram", binwidth = 0.1)
qplot(x = carat, data = diamonds, geom = "histogram", binwidth = 0.2)
qplot(x = carat, data = diamonds, geom = "histogram", binwidth = 0.2, fill = color)
qplot(x = carat, data = diamonds, geom = "density")
qplot(x = carat, data = diamonds, geom = "density", adjust = 0.5, xlim = c(0, 3))
## Warning: Removed 32 rows containing non-finite values (stat_density).
qplot(x = carat, data = diamonds, geom = "density", color = color, fill = color, alpha = I(1/5))
Time Series with Line and Path Plots
data("economics")
str(economics)
## 'data.frame': 478 obs. of 6 variables:
## $ date : Date, format: "1967-06-30" "1967-07-31" ...
## $ pce : num 508 511 517 513 518 ...
## $ pop : int 198712 198911 199113 199311 199498 199657 199808 199920 200056 200208 ...
## $ psavert : num 9.8 9.8 9 9.8 9.7 9.4 9 9.5 8.9 9.6 ...
## $ uempmed : num 4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
## $ unemploy: int 2944 2945 2958 3143 3066 3018 2878 3001 2877 2709 ...
qplot(x = date, y = unemploy / pop, data = economics, geom = "line")
qplot(x = date, y = uempmed, data = economics, geom = "line")
year1900 <- function(x) {
return(as.POSIXlt(x)$year + 1900)
}
qplot(x = unemploy / pop, y = uempmed, data = economics, geom = c("point", "path"))
qplot(x = unemploy / pop, y = uempmed, data = economics, geom = c("point", "path"), color = year1900(date))
qplot(x = unemploy / pop, y = uempmed, data = economics, geom = c("point", "path"), color = year1900(date)) + scale_size_area()