R和统计 3:ggplot2
1. qplot
library(ggplot2) dsmall <- diamonds[sample(nrow(diamonds), 100), ] # relationship qplot(carat, price, data = diamonds) qplot(carat,x*y*z,data = diamonds) # colour, shape qplot(carat, price, data = dsmall, colour = color) qplot(carat, price, data = dsmall, shape = cut) # opaque qplot(carat, price, data = diamonds, alpha = I(1/10)) qplot(carat, price, data = diamonds, alpha = I(1/100)) qplot(carat, price, data = diamonds, alpha = I(1/200)) # swig 1 not qplot(carat, price, data = dsmall, geom = c("point", "smooth"), span = 1) library(mgcv) qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "gam", formula= y ~ s(x, bs = "cs")) library(splines) qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "lm") qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "lm", formula= y ~ ns(x,5)) # jitter qplot(color, price / carat, data = diamonds, geom = "jitter", alpha = I(1 / 200)) # histogram qplot(carat, data = diamonds, geom = "histogram") qplot(carat, data = diamonds, geom = "density") qplot(carat, data = diamonds, geom = "histogram", binwidth = 1, xlim = c(0,3)) qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.1, xlim = c(0,3)) qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.01, xlim = c(0,3)) qplot(carat, data = diamonds, geom = "density", colour = color) qplot(carat, data = diamonds, geom = "histogram", fill = color) #important qplot(color, data = diamonds, geom = "bar") qplot(color, data = diamonds, geom = "bar", weight = carat) + scale_y_continuous("carat") # time series qplot(date, unemploy / pop, data = economics, geom = "line") # important qplot(date, uempmed, data = economics, geom = "line") year <- function(x) as.POSIXlt(x)$year + 1900 qplot(unemploy / pop, uempmed, data = economics, geom = c("point", "path")) qplot(unemploy / pop, uempmed, data = economics, geom = "path", colour = year(date)) + scale_area() # facet qplot(carat, data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1, xlim = c(0, 3)) qplot(carat, ..density.., data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1, xlim = c(0, 3)) data(mpg) qplot(displ, hwy, data = mpg, colour = factor(cyl)) qplot(displ, hwy, data=mpg, facets=.~year) + geom_smooth()
2. 了解qplot的强大以后,可以开始了解ggplot了
A plot object is a list with components data, mapping(the default aesthetic mappings),layers, scales, coordinates and facet.
# ggplot ggplot(msleep, aes(sleep_rem / sleep_total, awake)) + geom_point() + geom_smooth() bestfit <- geom_smooth(method = "lm", se = F, colour = alpha("steelblue", 0.5), size = 2) qplot(sleep_rem, sleep_total, data = msleep) + bestfit qplot(awake, brainwt, data = msleep, log = "y") + bestfit qplot(bodywt, brainwt, data = msleep, log = "xy") + bestfit p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point() p mtcars <- transform(mtcars, mpg = mpg ^ 2) p %+% mtcars aes(x = weight,y=height, colour = age) layer(geom, geom_params, stat, stat_params, data, mapping, position) # forward p <- ggplot(Oxboys, aes(age, height, group = Subject)) + geom_line() p + geom_smooth(aes(group = Subject), method="lm", se = F) p + geom_smooth(aes(group = 1), method="lm", size = 2, se = F) d <- ggplot(diamonds, aes(carat)) + xlim(0, 3) d + stat_bin(aes(ymax = ..count..), binwidth = 0.1, geom = "area") d + stat_bin( aes(size = ..density..), binwidth = 0.1, geom = "point", position="identity") d + stat_bin( aes(y = 1, fill = ..count..), binwidth = 0.1, geom = "tile", position="identity" )