anscombe library(graphics) # not really needed plot(y1 ~ x1, data = anscombe, pch = 16) ## this line is not really needed, as the graphics package is already ## attached when R starts library(lattice) xyplot(y1 ~ x1, data = anscombe, pch = 16) library(ggplot2) ggplot(data = anscombe) + geom_point(mapping = aes(x = x1, y = y1)) par(mfrow = c(2, 2)) plot(y1 ~ x1, data = anscombe, pch = 16) plot(y2 ~ x2, data = anscombe, pch = 16) plot(y3 ~ x3, data = anscombe, pch = 16) plot(y4 ~ x4, data = anscombe, pch = 16) anscombe.long <- with(anscombe, data.frame(x = c(x1, x2, x3, x4), y = c(y1, y2, y3, y4), which = rep(c("A", "B", "C", "D"), each = 11))) anscombe.long xyplot(y ~ x | which, data = anscombe.long, pch = 16, grid = TRUE) ggplot(data = anscombe.long) + facet_wrap(~ which) + geom_point(aes(x = x, y = y)) for (w in c("A", "B", "C", "D")) { plot(y ~ x, data = anscombe.long, pch = 16, subset = (which == w)) abline(coef(lm(y ~ x, data = anscombe.long, subset = (which == w)))) } ggplot(data = anscombe.long) + facet_wrap(~ which) + geom_smooth(aes(x = x, y = y), method = "lm") ggplot(data = anscombe.long) + facet_wrap(~ which) + geom_smooth(aes(x = x, y = y), method = "lm", se = FALSE) + geom_point(aes(x = x, y = y)) xyplot(y ~ x | which, data = anscombe.long, pch = 16, panel = panel.points) xyplot(y ~ x | which, data = anscombe.long, pch = 16, panel = panel.lmline) displayFunction <- function(x, y) { panel.grid(h = -1, v = -1) ## add a reference grid panel.points(x, y, pch = 16) ## draw the points panel.lmline(x, y, col = "grey50") ## draw linear regression line } xyplot(y ~ x | which, data = anscombe.long, panel = displayFunction) data(NHANES, package = "NHANES") lattice::histogram( ~ BPSysAve | Gender, data = NHANES, layout = c(1, 2), nint = 25, type = "density", as.table = TRUE) p <- ggplot(data = NHANES) + facet_wrap(~ Gender, ncol = 1) p + stat_bin(aes(x = BPSysAve), bins = 25, geom = "point") p + stat_bin(aes(x = BPSysAve), bins = 25, geom = "line") p + stat_bin(aes(x = BPSysAve, y = after_stat(density)), bins = 25, geom = "line") p + stat_density(aes(x = BPSysAve)) p + stat_density(aes(x = BPSysAve), geom = "line") p + stat_density(aes(x = BPSysAve, color = Race1), geom = "line") p + stat_density(aes(x = 1/BPSysAve, fill = Race1), geom = "area") p + stat_density(aes(x = 1/BPSysAve, color = Race1), geom = "line", position = "identity") gapminder <- read.delim("data/gapminder.tsv") str(gapminder) pgm <- ggplot(data = gapminder) + facet_wrap(~ year) pgm + geom_point(aes(x = gdpPercap, y = lifeExp)) pgm + geom_point(aes(x = log(gdpPercap), y = lifeExp)) pgm + geom_point(aes(x = log(gdpPercap), y = lifeExp, color = continent)) pgm + geom_point()) ## RGL library(rgl) with(subset(gapminder, year == 2002), plot3d(sqrt(pop), lifeExp, log(gdpPercap), type = "s", size = 1)) ## plotly q <- ggplot(data = gapminder, mapping = aes(x = log(gdpPercap), y = lifeExp, size = pop, color = continent, label = country)) + facet_wrap(~ year) + geom_point() library(plotly) ggplotly(p)