| 1 |
library(ggplot2) |
| 2 |
library(lattice) |
| 3 |
|
| 4 |
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3")) |
| 5 |
values <- data.frame( |
| 6 |
id = ids, |
| 7 |
value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5) |
| 8 |
) |
| 9 |
positions <- data.frame( |
| 10 |
id = rep(ids, each = 4), |
| 11 |
x = c( |
| 12 |
2, |
| 13 |
1, |
| 14 |
1.1, |
| 15 |
2.2, |
| 16 |
1, |
| 17 |
0, |
| 18 |
0.3, |
| 19 |
1.1, |
| 20 |
2.2, |
| 21 |
1.1, |
| 22 |
1.2, |
| 23 |
2.5, |
| 24 |
1.1, |
| 25 |
0.3, |
| 26 |
0.5, |
| 27 |
1.2, |
| 28 |
2.5, |
| 29 |
1.2, |
| 30 |
1.3, |
| 31 |
2.7, |
| 32 |
1.2, |
| 33 |
0.5, |
| 34 |
0.6, |
| 35 |
1.3 |
| 36 |
), |
| 37 |
y = c( |
| 38 |
-0.5, |
| 39 |
0, |
| 40 |
1, |
| 41 |
0.5, |
| 42 |
0, |
| 43 |
0.5, |
| 44 |
1.5, |
| 45 |
1, |
| 46 |
0.5, |
| 47 |
1, |
| 48 |
2.1, |
| 49 |
1.7, |
| 50 |
1, |
| 51 |
1.5, |
| 52 |
2.2, |
| 53 |
2.1, |
| 54 |
1.7, |
| 55 |
2.1, |
| 56 |
3.2, |
| 57 |
2.8, |
| 58 |
2.1, |
| 59 |
2.2, |
| 60 |
3.3, |
| 61 |
3.2 |
| 62 |
) |
| 63 |
) |
| 64 |
huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron)) |
| 65 |
|
| 66 |
ggplot2_examples <- list( |
| 67 |
GeomPointColor = { |
| 68 |
ggplot(mtcars, aes(wt, mpg, color = cyl)) + geom_point() |
| 69 |
}, |
| 70 |
GeomAbline = { |
| 71 |
ggplot(mtcars, aes(mpg, wt)) + |
| 72 |
geom_point() + |
| 73 |
geom_hline( |
| 74 |
aes(yintercept = wt), |
| 75 |
data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00)) |
| 76 |
) + |
| 77 |
facet_wrap(~cyl) |
| 78 |
}, |
| 79 |
GeomBar = ggplot(mpg, aes(class)) + geom_bar(aes(weight = displ)), |
| 80 |
GeomBin2d = ggplot(diamonds, aes(x, y)) + |
| 81 |
xlim(4, 10) + |
| 82 |
ylim(4, 10) + |
| 83 |
geom_bin2d(), |
| 84 |
GeomBox = ggplot(mpg, aes(class, hwy)) + geom_boxplot(), |
| 85 |
GeomContour = ggplot(faithfuld, aes(waiting, eruptions, z = density)) + |
| 86 |
geom_contour(), |
| 87 |
GeomContourRaster = ggplot(faithfuld, aes(waiting, eruptions, z = density)) + |
| 88 |
geom_raster(aes(fill = density)) + |
| 89 |
geom_contour(), |
| 90 |
GeomCount = ggplot(mpg, aes(cty, hwy)) + geom_count(), |
| 91 |
GeomDensity = ggplot(diamonds, aes(carat)) + geom_density(), |
| 92 |
GeomDensityColor = ggplot(diamonds, aes(depth, fill = cut, colour = cut)) + |
| 93 |
geom_density(alpha = 0.1), |
| 94 |
GeomDotPlot = ggplot(mtcars, aes(x = mpg)) + geom_dotplot(), |
| 95 |
GeomError = { |
| 96 |
ggplot( |
| 97 |
data.frame( |
| 98 |
trt = factor(c(1, 1, 2, 2)), |
| 99 |
resp = c(1, 5, 3, 4), |
| 100 |
group = factor(c(1, 2, 1, 2)), |
| 101 |
se = c(0.1, 0.3, 0.3, 0.2) |
| 102 |
), |
| 103 |
aes(resp, trt, colour = group) |
| 104 |
) + |
| 105 |
geom_point() + |
| 106 |
geom_errorbarh(aes(xmax = resp + se, xmin = resp - se)) |
| 107 |
}, |
| 108 |
GeomHex = ggplot(diamonds, aes(carat, price)) + |
| 109 |
geom_hex(binwidth = c(.1, 500)), |
| 110 |
GeomHistogram = ggplot(diamonds, aes(price, fill = cut)) + |
| 111 |
geom_histogram(binwidth = 500), |
| 112 |
GeomJitter = ggplot(mpg, aes(cyl, hwy)) + geom_jitter(width = 0.25), |
| 113 |
GeomErrorbar = ggplot( |
| 114 |
data.frame( |
| 115 |
trt = factor(c(1, 1, 2, 2)), |
| 116 |
resp = c(1, 5, 3, 4), |
| 117 |
group = factor(c(1, 2, 1, 2)), |
| 118 |
upper = c(1.1, 5.3, 3.3, 4.2), |
| 119 |
lower = c(0.8, 4.6, 2.4, 3.6) |
| 120 |
), |
| 121 |
aes(trt, resp, fill = group) |
| 122 |
) + |
| 123 |
geom_col(position = position_dodge(width = 0.9)) + |
| 124 |
geom_errorbar( |
| 125 |
aes(ymin = lower, ymax = upper), |
| 126 |
position = position_dodge(width = 0.9), |
| 127 |
width = 0.25 |
| 128 |
), |
| 129 |
GeomPolygon = ggplot(values) + |
| 130 |
geom_map(aes(map_id = id), map = positions) + |
| 131 |
expand_limits(positions), |
| 132 |
GeomPolygon2 = ggplot(values, aes(fill = value)) + |
| 133 |
geom_map(aes(map_id = id), map = positions) + |
| 134 |
expand_limits(positions), |
| 135 |
GeomLine = ggplot(economics_long, aes(date, value01, group = variable)) + |
| 136 |
geom_line(), |
| 137 |
GeomLine2 = ggplot(economics_long, aes(date, value01, colour = variable)) + |
| 138 |
geom_line(), |
| 139 |
GeomPoint = ggplot(diamonds, aes(carat, price)) + geom_point(alpha = 1 / 50), |
| 140 |
GeomPoint2 = ggplot( |
| 141 |
mtcars, |
| 142 |
aes(wt, mpg, color = factor(cyl), shape = factor(cyl)) |
| 143 |
) + |
| 144 |
geom_point(), |
| 145 |
GeomQuantile = ggplot(mpg, aes(displ, 1 / hwy)) + |
| 146 |
geom_point() + |
| 147 |
geom_quantile(), |
| 148 |
GeomRibbon = ggplot(huron, aes(year)) + |
| 149 |
geom_ribbon(aes(ymin = level - 1, ymax = level + 1), fill = "grey70") + |
| 150 |
geom_line(aes(y = level)), |
| 151 |
GeomRug = ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_rug(), |
| 152 |
GeomRug2 = ggplot(mtcars, aes(wt, mpg)) + |
| 153 |
geom_point() + |
| 154 |
geom_rug(outside = TRUE) + |
| 155 |
coord_cartesian(clip = "off"), |
| 156 |
GeomCurve = { |
| 157 |
df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 15.0) |
| 158 |
ggplot(mtcars, aes(wt, mpg)) + |
| 159 |
geom_point() + |
| 160 |
geom_curve( |
| 161 |
aes(x = x1, y = y1, xend = x2, yend = y2, colour = "curve"), |
| 162 |
data = df |
| 163 |
) + |
| 164 |
geom_segment( |
| 165 |
aes(x = x1, y = y1, xend = x2, yend = y2, colour = "segment"), |
| 166 |
data = df |
| 167 |
) + |
| 168 |
geom_curve( |
| 169 |
aes(x = x1, y = y1, xend = x2, yend = y2), |
| 170 |
data = df, |
| 171 |
curvature = -0.2 |
| 172 |
) |
| 173 |
}, |
| 174 |
GeomSmooth = ggplot( |
| 175 |
diamonds[sample(nrow(diamonds), 1000), ], |
| 176 |
aes(carat, price) |
| 177 |
) + |
| 178 |
geom_point(alpha = 0.2) + |
| 179 |
geom_smooth() + |
| 180 |
facet_wrap(~cut) + |
| 181 |
ggtitle("Diamond price by carat and cut"), |
| 182 |
GeomSmooth2 = ggplot(mpg, aes(displ, hwy, colour = class)) + |
| 183 |
geom_point() + |
| 184 |
geom_smooth(se = FALSE, method = lm), |
| 185 |
GeomSpoke = { |
| 186 |
df <- expand.grid(x = 1:10, y = 1:10) |
| 187 |
df$angle <- runif(100, 0, 2 * pi) |
| 188 |
df$speed <- runif(100, 0, sqrt(0.1 * df$x)) |
| 189 |
|
| 190 |
ggplot(df, aes(x, y)) + |
| 191 |
geom_point() + |
| 192 |
geom_spoke(aes(angle = angle), radius = 0.5) |
| 193 |
}, |
| 194 |
GeomText = ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) + |
| 195 |
geom_text(), |
| 196 |
GeomLabel = ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) + |
| 197 |
geom_label(), |
| 198 |
GeomTile = ggplot( |
| 199 |
data.frame( |
| 200 |
x = rep(c(2, 5, 7, 9, 12), 2), |
| 201 |
y = rep(c(1, 2), each = 5), |
| 202 |
z = factor(rep(1:5, each = 2)), |
| 203 |
w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2) |
| 204 |
), |
| 205 |
aes(x, y, width = w) |
| 206 |
) + |
| 207 |
geom_tile(aes(fill = z)), |
| 208 |
GeomViolin = ggplot(mtcars, aes(factor(cyl), mpg)) + geom_violin() |
| 209 |
) |
| 210 |
|
| 211 |
lattice_examples <- list( |
| 212 |
settings = quote(show.settings()), |
| 213 |
densityplot = quote( |
| 214 |
densityplot( |
| 215 |
~mpg, |
| 216 |
data = mtcars, |
| 217 |
group = factor( |
| 218 |
mtcars$am, |
| 219 |
levels = c(0, 1), |
| 220 |
labels = c("Automatic", "Manual") |
| 221 |
), |
| 222 |
main = "MPG Distribution by Transmission Type", |
| 223 |
xlab = "Miles per Gallon", |
| 224 |
auto.key = TRUE |
| 225 |
) |
| 226 |
) |
| 227 |
) |
| 228 |
|
| 229 |
base_examples <- list( |
| 230 |
scatterplot = quote( |
| 231 |
plot( |
| 232 |
iris$Sepal.Length, |
| 233 |
iris$Sepal.Width, |
| 234 |
col = iris$Species, |
| 235 |
main = "Sepal Length vs Width in Iris" |
| 236 |
) |
| 237 |
), |
| 238 |
hist = quote(hist(iris$Sepal.Length)) |
| 239 |
) |