-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplot.R
1430 lines (1194 loc) · 56.1 KB
/
ggplot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#source('Rstart.R')
#setwd('~/Google Drive/R/')
require('ggplot2')
require('plyr')
require('reshape2')
### Setting up backgound defaults
# change the default background color
old.theme <- theme_set(theme_bw())
## To change the panel's background color, use the following code:
# myplot + opts(panel.background = theme_rect(fill='green', colour='red'))
## To change the color of the plot (but not the color of the panel), you can do:
# myplot + opts(plot.background = theme_rect(fill='green', colour='red'))
set.seed(1410) # Make the sample reproducible
dsmall <- diamonds[sample(nrow(diamonds), 100), ]
qplot(carat, price, data = diamonds)
qplot(log(carat), log(price), data = diamonds)
qplot(carat, x * y * z, data = diamonds)
# Mapping point colour to diamond colour (left), and point shape to cut
# quality (right).
qplot(carat, price, data = dsmall, colour = color)
qplot(carat, price, data = dsmall, shape = cut)
# contrast this with plot()
# default color palette in R: palette()
#[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow"
#[8] "gray"
# starting from red, as opposed to default black
png('iris.png', 640, 480)
par (mar=c(3,3,2,1), mgp=c(2,.7,0), tck=-.012, las=1)
with(iris, plot(Sepal.Length, Sepal.Width, col=as.numeric(Species)+1, pch=20))
lbs <- levels(iris$Species)
legend('topright', legend=lbs,
col=2:4, cex=0.7, pch=20, box.lwd=0.5, pt.cex=0.6)
dev.off()
# now with qplot in one line of code
png('irisq.png', 640, 480)
qplot(Sepal.Length, Sepal.Width, data = iris, colour = Species, xlim=c(4,8))
dev.off()
# Reducing the alpha value from 1/10 (left) to 1/100 (middle) to 1/200
# (right) makes it possible to see where the bulk of the points lie.
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))
# Smooth curves add to scatterplots of carat vs.\ price. The dsmall
# dataset (left) and the full dataset (right).
# If you want to turn the confidence interval off, use se = FALSE.
qplot(carat, price, data = dsmall, geom = c("point", "smooth"))
qplot(carat, price, data = diamonds, geom = c("point", "smooth"))
# The effect of the span parameter. (Left) \code{span = 0.2}, and
# (right) \code{span = 1}.
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
span = 0.2)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
span = 1)
# The effect of the formula parameter, using a generalised additive
# model as a smoother. (Left) \code{formula = y ~ s(x)}, the default;
# (right) \code{formula = y ~ s(x, bs = "cs")}.
library(mgcv)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "gam", formula = y ~ s(x))
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "gam", formula = y ~ s(x, bs = "cs"))
# The effect of the formula parameter, using a linear model as a
# smoother. (Left) \code{formula = y ~ x}, the default; (right)
# \code{formula = y ~ ns(x, 5)}.
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))
# Using jittering (left) and boxplots (right) to investigate the
# distribution of price per carat, conditional on colour. As the
# colour improves (from left to right) the spread of values decreases,
# but there is little change in the centre of the distribution.
qplot(color, price / carat, data = diamonds, geom = "jitter")
qplot(color, price / carat, data = diamonds, geom = "boxplot")
# Varying the alpha level. From left to right: $1/5$, $1/50$, $1/200$.
# As the opacity decreases we begin to see where the bulk of the data
# lies. However, the boxplot still does much better.
qplot(color, price / carat, data = diamonds, geom = "jitter",
alpha = I(1 / 5))
qplot(color, price / carat, data = diamonds, geom = "jitter",
alpha = I(1 / 50))
qplot(color, price / carat, data = diamonds, geom = "jitter",
alpha = I(1 / 200))
# Displaying the distribution of diamonds. (Left) \code{geom =
# "histogram"} and (right) \code{geom = "density"}.
qplot(carat, data = diamonds, geom = "histogram")
qplot(carat, data = diamonds, geom = "density")
# Varying the bin width on a histogram of carat reveals interesting
# patterns. Binwidths from left to right: 1, 0.1 and 0.01 carats. Only
# diamonds between 0 and 3 carats shown.
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))
# Mapping a categorical variable to an aesthetic will automatically
# split up the geom by that variable. (Left) Density plots are
# overlaid and (right) histograms are stacked.
qplot(carat, data = diamonds, geom = "density", colour = color)
qplot(carat, data = diamonds, geom = "histogram", fill = color)
# Change the background color
qplot(Sepal.Width, data = iris, geom = "density",
main='Sepal Width Densities', color = Species) +
opts(panel.background = theme_rect(fill='white'))
qplot(Sepal.Width, data = iris, geom = "density",
main='Sepal Width Densities', color = Species) + theme_bw()
# Bar charts of diamond colour. The left plot shows counts and the
# right plot is weighted by \code{weight = carat} to show the total
# weight of diamonds of each colour.
qplot(color, data = diamonds, geom = "bar")
qplot(color, data = diamonds, geom = "bar", weight = carat) +
scale_y_continuous("carat")
# Two time series measuring amount of unemployment. (Left) Percent of
# population that is unemployed and (right) median number of weeks
# unemployed. Plots created with {\tt geom="line"}.
qplot(date, unemploy / pop, data = economics, geom = "line")
qplot(date, uempmed, data = economics, geom = "line")
# Path plots illustrating the relationship between percent of people
# unemployed and median length of unemployment. (Left) Scatterplot
# with overlaid path. (Right) Pure path plot coloured by year.
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() + theme_bw()
# Histograms showing the distribution of carat conditional on colour.
# (Left) Bars show counts and (right) bars show densities (proportions
# of the whole). The density plot makes it easier to compare
# distributions ignoring the relative abundance of diamonds within each
# colour. High-quality diamonds (colour D) are skewed towards small
# sizes, and as quality declines the distribution becomes more flat.
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))
qplot(Sepal.Width, ..density.., data = iris, facets = Species ~ .,
geom = "histogram", binwidth = 0.1)
qplot(Sepal.Width, data = iris, facets = Species ~ .,
geom = "density")
qplot(
carat, price, data = dsmall,
xlab = "Price ($)", ylab = "Weight (carats)",
main = "Price-weight relationship"
)
qplot(
carat, price/carat, data = dsmall,
ylab = expression(frac(price,carat)),
xlab = "Weight (carats)",
main="Small diamonds",
xlim = c(.2,1)
)
qplot(carat, price, data = dsmall, log = "xy")
# A scatterplot of engine displacement in litres (displ) vs. average
# highway miles per gallon (hwy). Points are coloured according to
# number of cylinders. This plot summarises the most important factor
# governing fuel economy: engine size.
qplot(displ, hwy, data = mpg, colour = factor(cyl))
# size, a colour and a shape. These attributes are called aesthetics
# Points, lines and bars are all examples of geometric objects, or geoms.
# Geoms determine the “type” of the plot
# More complicated plots don't have their own names. This plot takes
# Figure~\ref{fig:mpg} and adds a regression line to each group. What
# would you call this plot?
qplot(displ, hwy, data=mpg, colour=factor(cyl)) +
geom_smooth(data=subset(mpg, cyl != 5), method="lm") + theme_bw()
# A more complex plot with facets and multiple layers.
qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth() + theme_bw()
qplot(Sepal.Length, Petal.Width, data=iris, facets = . ~ Species) +
geom_smooth(method='lm') + theme_bw()
# Together, the data, mappings, stat, geom and position adjustment form a layer.
# Examples of legends from four different scales. From left to right:
# continuous variable mapped to size, and to colour, discrete variable
# mapped to shape, and to colour. The ordering of scales seems
# upside-down, but this matches the labelling of the $y$-axis: small
# values occur at the bottom.
x <- 1:10
y <- factor(letters[1:5])
qplot(x, x, size = x) + opts(keep = "legend_box")
qplot(x, x, 1:10, colour = x) + opts(keep = "legend_box")
qplot(y, y, 1:10, shape = y) + opts(keep = "legend_box")
qplot(y, y, 1:10, colour = y) + opts(keep = "legend_box")
# Examples of axes and grid lines for three coordinate systems:
# Cartesian, semi-log and polar. The polar coordinate system
# illustrates the difficulties associated with non-Cartesian
# coordinates: it is hard to draw the axes well.
x1 <- c(1,10)
y1 <- c(1, 5)
p <- qplot(x1, y1, geom="blank", xlab=NULL, ylab=NULL)
p
p + coord_trans(y="log10")
p + coord_polar()
p <- qplot(displ, hwy, data = mpg, colour = factor(cyl))
summary(p)
p
# Save plot object to disk
save(p, file = "plot.rdata")
# Load from disk
load("plot.rdata")
# Save png to disk
ggsave("plot.pdf", width = 8, height = 5)
# Chapter 4: Build a Plot layer by layer
# qplot(), permits only a single dataset and a single set of
# aesthetic mappings. Need ggplot()
p <- ggplot(diamonds, aes(carat, price, colour = cut))
p <- p + layer(geom = "point")
p
# layer(geom, geom_params, stat, stat_params, data, mapping, position)
p <- ggplot(diamonds, aes(x=carat))
p <- p + layer(
geom = "bar",
geom_params = list(fill = "steelblue"),
stat = "bin",
stat_params = list(binwidth = 2)
)
p
# same as above
p <- ggplot(diamonds, aes(x=carat))
p <- p + geom_histogram(binwidth = 2, fill = "steelblue")
p
ggplot(msleep, aes(sleep_rem / sleep_total, awake)) +
geom_point()
# which is equivalent to
qplot(sleep_rem / sleep_total, awake, data = msleep)
# You can add layers to qplot too:
qplot(sleep_rem / sleep_total, awake, data = msleep) +
geom_smooth()
# This is equivalent to
qplot(sleep_rem / sleep_total, awake, data = msleep,
geom = c("point", "smooth"))
# or
ggplot(msleep, aes(sleep_rem / sleep_total, awake)) +
geom_point() + geom_smooth()
p <- ggplot(msleep, aes(sleep_rem / sleep_total, awake))
summary(p)
p <- p + geom_point()
summary(p)
# best fit line
require(scales)
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
# modifying the plot by simply changing the original dataframe
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)
aes(weight, height, colour = sqrt(age))
p <- ggplot(mtcars)
summary(p)
p <- p + aes(wt, hp)
summary(p)
p <- ggplot(mtcars, aes(x = mpg, y = wt))
p + geom_point()
# Overriding aesthetics. (Left) Overriding colour with {\tt
# factor(cyl)} and (right) overriding y-position with {\tt disp}
p + geom_point(aes(colour = factor(cyl)))
p + geom_point(aes(y = disp))
# difference between setting and mapping
p <- ggplot(mtcars, aes(mpg, wt))
p + geom_point(colour = "darkblue")
p + geom_point(aes(colour = "darkblue"))
# The difference between (left) setting colour to \code{"darkblue"} and
# (right) mapping colour to \code{"darkblue"}. When \code{"darkblue"}
# is mapped to colour, it is treated as a regular value and scaled with
# the default colour scale. This results in pinkish points and a
# legend.
qplot(mpg, wt, data=mtcars, colour = I("darkblue"))
qplot(mpg, wt, data=mtcars, colour = "darkblue")
# (Left) Correctly specifying {\tt group = Subject} produces one line
# per subject. (Right) A single line connects all observations. This
# pattern is characteristic of an incorrect grouping aesthetic, and is
# what we see if the group aesthetic is omitted, which in this case is
# equivalent to {\tt group = 1}.
data(Oxboys, package="nlme")
p <- ggplot(Oxboys, aes(age, height, group = Subject)) +
geom_line()
p
qplot(age, height, data=Oxboys, group = Subject, geom="line")
qplot(age, height, data=Oxboys, geom="line")
p + geom_smooth(aes(group = Subject), method="lm", se = F)
p + geom_smooth(aes(group = 1), method="lm", size = 2, se = F)
boysbox <- ggplot(Oxboys, aes(Occasion, height)) + geom_boxplot()
boysbox
boysbox + geom_line(aes(group = Subject), colour = "#3366FF")
# (Left) If boxplots are used to look at the distribution of heights at
# each occasion (a discrete variable), the default grouping works
# correctly. (Right) If trajectories of individual boys are overlaid
# with {\tt geom\_line()}, then {\tt aes(group = Subject)} is needed
# for the new layer.
qplot(Occasion, height, data=Oxboys, geom="boxplot")
qplot(Occasion, height, data=Oxboys, geom="boxplot") +
geom_line(aes(group = Subject), colour="#3366FF")
# For lines and paths, the aesthetics of the line segment are
# determined by the aesthetic of the beginning observation. If colour
# is categorical (left) there is no meaningful way to interpolate
# between adjacent colours. If colour is continuous (right), there is,
# but this is not done by default.
df <- data.frame(x = 1:3, y = 1:3, colour = c(1,3,5))
qplot(x, y, data=df, colour=factor(colour), size = I(5)) +
geom_line(aes(group = 1), size = 2)
qplot(x, y, data=df, colour=colour, size = I(5)) + geom_line(size = 2)
xgrid <- with(df, seq(min(x), max(x), length = 50))
interp <- data.frame(
x = xgrid,
y = approx(df$x, df$y, xout = xgrid)$y,
colour = approx(df$x, df$colour, xout = xgrid)$y
)
qplot(x, y, data = df, colour = colour, size = I(5)) +
geom_line(data = interp, size = 2)
# Splitting apart a bar chart (left) produces a plot (right) that has
# the same outline as the original.
qplot(color, data = diamonds)
qplot(color, data = diamonds, fill = cut)
ggplot(diamonds, aes(carat)) +
geom_histogram(aes(y = ..density..), binwidth = 0.1)
# same as above
qplot(carat, ..density.., data = diamonds, geom="histogram",
binwidth = 0.1)
# Three position adjustments applied to a bar chart. From left to
# right, stacking, filling and dodging.
dplot <- ggplot(diamonds, aes(clarity, fill = cut))
dplot + geom_bar(position = "stack")
dplot + geom_bar(position = "fill")
dplot + geom_bar(position = "dodge")
# The identity positon adjustment is not useful for bars, (left)
# because each bar obscures the bars behind. (Right) It is useful for
# lines, however, because lines do not have the same problem.
dplot + geom_bar(position = "identity")
qplot(clarity, data = diamonds, geom="line", colour = cut,
stat="bin", group=cut)
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"
)
# Three variations on the histogram. (Left) A frequency polygon;
# (middle) a scatterplot with both size and height mapped to frequency;
# (right) a heatmap representing frequency with colour.
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") + scale_y_continuous("")
require(nlme, quiet = TRUE, warn.conflicts = FALSE)
model <- lme(height ~ age, data = Oxboys,
random = ~ 1 + age | Subject)
oplot <- ggplot(Oxboys, aes(age, height, group = Subject)) +
geom_point() #change to geam_line()
age_grid <- seq(-1, 1, length = 10)
subjects <- unique(Oxboys$Subject)
preds <- expand.grid(age = age_grid, Subject = subjects)
preds$height <- predict(model, preds)
oplot + geom_line(data = preds, colour = "#3366FF", size= 0.4)
Oxboys$fitted <- predict(model)
Oxboys$resid <- with(Oxboys, fitted - height)
oplot %+% Oxboys + aes(y = resid) + geom_smooth(aes(group=1))
model2 <- update(model, height ~ age + I(age ^ 2))
Oxboys$fitted2 <- predict(model2)
Oxboys$resid2 <- with(Oxboys, fitted2 - height)
oplot %+% Oxboys + aes(y = resid2) + geom_smooth(aes(group=1))
## Chapter 5, Toolbox
library(effects)
# The basic geoms applied to the same data. Many give rise to to named
# plots (from top left to bottom right): scatterplot, bar chart, line
# chart, area chart, path plot, labelled scatterplot, image/level plot
# and polygon plot. Observe the different axis ranges for the bar,
# area and tile plots: these geoms take up space outside the range of
# the data, and so push the axes out.
df <- data.frame(
x = c(3, 1, 5),
y = c(2, 4, 6),
label = c("a","b","c")
)
p <- ggplot(df, aes(x, y, label = label)) +
xlab(NULL) + ylab(NULL)
p + geom_point() + opts(title = "geom_point")
p + geom_bar(stat="identity") +
opts(title = "geom_bar(stat=\"identity\")")
p + geom_line() + opts(title = "geom_line")
p + geom_area() + opts(title = "geom_area")
p + geom_path() + opts(title = "geom_path")
p + geom_text() + opts(title = "geom_text")
p + geom_tile() + opts(title = "geom_tile")
p + geom_polygon() + opts(title = "geom_polygon")
# (Left) Never rely on the default parameters to get a revealing view
# of the distribution. (Right) Zooming in on the x axis, {\tt xlim =
# c(55, 70)}, and selecting a smaller bin width, {\tt binwidth = 0.1},
# reveals far more detail. We can see that the distribution is slightly
# skew-right. Don't forget to include information about important
# parameters (like bin width) in the caption.
theme_set(old.theme)
qplot(depth, data=diamonds, geom="histogram")
qplot(depth, data=diamonds, geom="histogram", xlim=c(55, 70), binwidth=0.1)
old.theme <- theme_set(theme_bw())
# Three views of the distribution of depth and cut. From top to
# bottom: faceted histogram, a conditional density plot, and frequency
# polygons. All show an interesting pattern: as quality increases, the
# distribution shifts to the left and becomes more symmetric.
depth_dist <- ggplot(diamonds, aes(depth)) + xlim(58, 68)
depth_dist +
geom_histogram(aes(y = ..density..), binwidth = 0.1) +
facet_grid(cut ~ .)
depth_dist + geom_histogram(aes(fill = cut), binwidth = 0.1,
position = "fill")
depth_dist + geom_freqpoly(aes(y = ..density.., colour = cut),
binwidth = 0.5)
# The boxplot geom can be use to see the distribution of a continuous
# variable conditional on a discrete varable like cut (left), or
# continuous variable like carat (right). For continuous variables,
# the group aesthetic must be set to get multiple boxplots. Here {\tt
# group = round\_any(carat, 0.1, floor)} is used to get a boxplot for
# each 0.1 carat bin.
qplot(cut, depth, data=diamonds, geom="boxplot")
qplot(carat, depth, data=diamonds, geom="boxplot",
group = round_any(carat, 0.1, floor), xlim = c(0, 3))
# The jitter geom can be used to give a crude visualisation of 2d
# distributions with a discrete component. Generally this works better
# for smaller datasets. Car class vs. continuous variable city mpg
# (top) and discrete variable drive train (bottom).
qplot(class, cty, data=mpg, geom="jitter")
qplot(class, drv, data=mpg, geom="jitter")
# The density plot is a smoothed version of the histogram. It has
# desirable theoretical properties, but is more difficult to relate
# back to the data. A density plot of depth (left), coloured by cut
# (right).
qplot(depth, data=diamonds, geom="density", xlim = c(54, 70))
qplot(depth, data=diamonds, geom="density", xlim = c(54, 70),
fill = cut, alpha = I(0.2))
# Modifying the glyph used can help with mild to moderate overplotting.
# From left to right: the default shape, {\tt shape = 1} (hollow
# points), and {\tt shape= "."} (pixel points).
df <- data.frame(x = rnorm(5000), y = rnorm(5000))
norm <- ggplot(df, aes(x, y))
norm + geom_point()
norm + geom_point(shape = 4, colour='red')
norm + geom_point(shape = ".") # Pixel sized
# Using alpha blending to alleviate overplotting in sample data from a
# bivariate normal. Alpha values from left to right: 1/3, 1/5, 1/10.
norm + geom_point(colour = "black", alpha = 1/3)
norm + geom_point(colour = "black", alpha = 1/5)
norm + geom_point(colour = "black", alpha = 1/10)
# A plot of table vs. depth from the diamonds data, showing the use of
# jitter and alpha blending to alleviate overplotting in discrete data.
# From left to right: geom point, geom jitter with default jitter, geom
# jitter with horizontal jitter of 0.5 (half the gap between bands),
# alpha of 1/10, alpha of 1/50, alpha of 1/200.
td <- ggplot(diamonds, aes(table, depth)) +
xlim(50, 70) + ylim(50, 70)
td + geom_point()
td + geom_jitter()
jit <- position_jitter(width = 0.5)
td + geom_jitter(position = jit)
td + geom_jitter(position = jit, colour = "black", alpha = 1/3)
td + geom_jitter(position = jit, colour = "black", alpha = 1/5)
td + geom_jitter(position = jit, colour = "black", alpha = 1/10)
# Binning with, top row, square bins, and bottom row, hexagonal bins.
# Left column uses default parameters, middle column {\tt bins = 10},
# and right column {\tt binwidth = c(0.02, 200)}. Legends have been
# omitted to save space.
d <- ggplot(diamonds, aes(carat, price)) +
opts(legend.position = "none")
d + stat_bin2d()
d + stat_bin2d(bins = 10) + scale_colour_gradient(high = "red")
d + stat_bin2d(binwidth=c(0.02, 200))
d + stat_binhex()
d + stat_binhex(bins = 10)
d + stat_binhex(binwidth=c(0.02, 200))
# Using density estimation to model and visualise point densities.
# (Top) Image displays of the density; (bottom) point and contour based
# displays.
d <- ggplot(diamonds, aes(carat, price)) +
opts(legend.position = "none")
d + geom_point() + geom_density2d()
d + stat_density2d(geom = "point", aes(size = ..density..),
contour = F) + scale_area(0.2, 1.5)
d + stat_density2d(geom = "tile", aes(fill = ..density..),
contour = F)
last_plot() + scale_fill_gradient(limits = c(1e-5,8e-4))
d <- ggplot(iris, aes(Sepal.Length, Petal.Width))
d + geom_point() + geom_density2d()
d <- ggplot(iris, aes(Sepal.Length, Petal.Width))
d + geom_point() + geom_density2d(aes(Petal.Length))
require(rgl)
with(iris, plot3d(Sepal.Length, Petal.Width, Petal.Length, col=Species))
# Example using the borders function. (Left) All cities with
# population (as of January 2006) of greater than half a million,
# (right) cities in Texas.
library(maps)
data(us.cities)
big_cities <- subset(us.cities, pop > 500000)
qplot(long, lat, data = big_cities) + borders("state", size = 0.1) +
scale_x_continuous(breaks = NA) + scale_y_continuous(breaks = NA) +
xlab("") + ylab("")
tx_cities <- subset(us.cities, country.etc == "TX")
ggplot(tx_cities, aes(long, lat)) +
borders("county", "texas", colour = "grey70") +
geom_point(colour = "black", alpha = 0.5)
# Two choropleth maps showing number of assaults (left) and the ratio
# of assaults to murders (right).
states <- map_data("state")
arrests <- USArrests
names(arrests) <- tolower(names(arrests))
arrests$region <- tolower(rownames(USArrests))
choro <- merge(states, arrests, sort=FALSE, by = "region")
# Reorder the rows because order matters when drawing polygons
# and merge destroys the original ordering
choro <- choro[order(choro$order), ]
qplot(long, lat, data = choro, group = group,
fill = assault, geom = "polygon")
qplot(long, lat, data = choro, group = group,
fill = assault / murder, geom = "polygon")
qplot(long, lat, data = choro, group = group,
fill = rape / murder, geom = "polygon")
scale_colour_continuous <- function(...) scale_colour_gradient(low =
"blue", high = "red", na.value="grey50", ...)
ia <- map_data("county", "iowa")
mid_range <- function(x) mean(range(x, na.rm = TRUE))
centres <- ddply(ia, .(subregion),
colwise(mid_range, .(lat, long)))
ggplot(ia, aes(long, lat)) +
geom_polygon(aes(group = group),
fill = NA, colour = "grey60") +
geom_text(aes(label = subregion), data = centres,
size = 5, angle = 45)
d <- subset(diamonds, carat < 2.5 &
rbinom(nrow(diamonds), 1, 0.2) == 1)
d$lcarat <- log10(d$carat)
d$lprice <- log10(d$price)
# Remove overall linear trend
detrend <- lm(lprice ~ lcarat, data = d)
d$lprice2 <- resid(detrend)
mod <- lm(lprice2 ~ lcarat * color, data = d)
library(effects)
effectdf <- function(...) {
suppressWarnings(as.data.frame(effect(...)))
}
color <- effectdf("color", mod)
both1 <- effectdf("lcarat:color", mod)
carat <- effectdf("lcarat", mod, default.levels = 50)
both2 <- effectdf("lcarat:color", mod, default.levels = 3)
# Data transformed to remove most obvious effects. (Left) Both x and y
# axes are log10 transformed to remove non-linearity. (Right) The
# major linear trend is removed.
qplot(lcarat, lprice, data=d, colour = color)
qplot(lcarat, lprice2, data=d, colour = color)
# Displaying uncertainty in model estimates for colour. (Left)
# Marginal effect of colour. (Right) conditional effects of colour for
# different levels of carat. Error bars show 95\% pointwise confidence
# intervals.
fplot <- ggplot(mapping = aes(y = fit, ymin = lower, ymax = upper)) +
ylim(range(both2$lower, both2$upper))
fplot %+% color + aes(x = color) + geom_point() + geom_errorbar()
scale_colour_continuous <- function(...) scale_colour_gradient(low =
"blue", high = "red", na.value="grey50", ...)
fplot %+% both2 +
aes(x = color, colour = lcarat, group = interaction(color, lcarat)) +
geom_errorbar() + geom_line(aes(group=lcarat)) + scale_colour_continuous()
# scale_colour_gradient()
# Displaying uncertainty in model estimates for carat. (Left) marginal
# effect of carat. (Right) conditional effects of carat for different
# levels of colour. Bands show 95\% point-wise confidence intervals.
fplot %+% carat + aes(x = lcarat) + geom_smooth(stat="identity")
ends <- subset(both1, lcarat == max(lcarat))
fplot %+% both1 + aes(x = lcarat, colour = color) +
geom_smooth(stat="identity") +
scale_colour_hue() + opts(legend.position = "none") +
geom_text(aes(label = color, x = lcarat + 0.02), ends)
# Examples of \code{stat_summary} in use. (Top) Continuous x with,
# from left to right, median and line, \f{median_hilow} and smooth,
# mean and line, and \f{mean_cl_boot} and smooth. (Bottom) Discrete x
# with, from left to right, \f{mean} and point, \f{mean_cl_normal} and
# error bar, \f{median_hilow} and point range, and \f{median_hilow} and
# crossbar. Note that \ggplot displays the full range of the data, not
# just the range of the summary statistics.
m <- ggplot(movies, aes(year, rating))
m + stat_summary(fun.y = "median", geom = "line")
m + stat_summary(fun.data = "median_hilow", geom = "smooth")
m + stat_summary(fun.y = "mean", geom = "line")
m + stat_summary(fun.data = "mean_cl_boot", geom = "smooth")
m2 <- ggplot(movies, aes(round(rating), log10(votes)))
m2 + stat_summary(fun.y = "mean", geom = "point")
m2 + stat_summary(fun.data = "mean_cl_normal", geom = "errorbar")
m2 + stat_summary(fun.data = "median_hilow", geom = "pointrange")
m2 + stat_summary(fun.data = "median_hilow", geom = "crossbar")
midm <- function(x) mean(x, trim = 0.5)
m2 +
stat_summary(aes(colour = "trimmed"), fun.y = midm,
geom = "point") +
stat_summary(aes(colour = "raw"), fun.y = mean,
geom = "point") +
scale_colour_hue("Mean")
iqr <- function(x, ...) {
qs <- quantile(as.numeric(x), c(0.25, 0.75), na.rm = T)
names(qs) <- c("ymin", "ymax")
qs
}
m + stat_summary(fun.data = "iqr", geom="ribbon")
# plot annotations
unemp <- qplot(date, unemploy, data=economics, geom="line",
xlab = "", ylab = "No. unemployed (1000s)")
presidential <- presidential[-(1:3), ]
yrng <- range(economics$unemploy)
xrng <- range(economics$date)
#unemp + geom_vline(aes(xintercept = start), data = presidential)
require(scales)
unemp + geom_rect(aes(NULL, NULL, xmin = start, xmax = end,
fill = party), ymin = yrng[1], ymax = yrng[2], alpha=0.2,
data = presidential) + scale_fill_manual(values =
c("blue", "red"))
last_plot() + geom_text(aes(x = start, y = yrng[1], label = name),
data = presidential, size = 3, hjust = 0, vjust = 0)
caption <- paste(strwrap("Unemployment rates in the US have
varied a lot over the years", 40), collapse="\n")
unemp + geom_text(aes(x, y, label = caption),
data = data.frame(x = xrng[2], y = yrng[2]),
hjust = 1, vjust = 1, size = 4)
highest <- subset(economics, unemploy == max(unemploy))
unemp + geom_point(data = highest,
size = 3, colour = alpha("red", 0.5))
# Using size to display weights. No weighting (left), weighting by
# population (centre) and by area (right).
qplot(percwhite, percbelowpoverty, data = midwest)
qplot(percwhite, percbelowpoverty, data = midwest,
size = poptotal / 1e6) + scale_area("Population\n(millions)",
breaks = c(0.5, 1, 2, 4))
qplot(percwhite, percbelowpoverty, data = midwest, size = area) +
scale_area()
# An unweighted line of best fit (left) and weighted by population size
# (right).
lm_smooth <- geom_smooth(method = lm, size = 1)
qplot(percwhite, percbelowpoverty, data = midwest) + lm_smooth
qplot(percwhite, percbelowpoverty, data = midwest,
weight = popdensity, size = popdensity) + lm_smooth
# The difference between an unweighted (left) and weighted (right)
# histogram. The unweighted histogram shows number of counties, while
# the weighted histogram shows population. The weighting considerably
# changes the interpretation!
qplot(percbelowpoverty, data = midwest, binwidth = 1)
qplot(percbelowpoverty, data = midwest, weight = poptotal,
binwidth = 1) + ylab("population")
# scales, axes and legends
plot <- qplot(cty, hwy, data = mpg)
plot
# This doesn't work because there is a mismatch between the
# variable type and the default scale
plot + aes(x = drv)
# Correcting the default manually resolves the problem.
plot + aes(x = drv) + scale_x_discrete()
# Adjusting the default parameters of a scale. (Top left) The plot with
# default scale. (Top right) Adding the default scale by hand doesn't
# change the appearance of the plot. (Bottom left) Adjusting the
# parameters of the scale to tweak the legend. (Bottom right) Using a
# different colour scale: Set1 from the ColorBrewer colours.
p <- qplot(sleep_total, sleep_cycle, data = msleep, colour = vore)
p
# Explicitly add the default scale
p + scale_colour_hue()
# Adjust parameters of the default, here changing the appearance
# of the legend
p + scale_colour_hue("What does\nit eat?",
breaks = c("herbi", "carni", "omni", NA),
labels = c("plants", "meat", "both", "don't know"))
# Use a different scale
p + scale_colour_brewer(palette = "Set2")
# try my own
p <- qplot(Petal.Length, Sepal.Width, data=iris, colour=Species)
p
p + scale_colour_hue()
p + scale_colour_brewer(palette = "Set1")
# A demonstration of the different forms legend title can take.
p <- qplot(cty, hwy, data = mpg, colour = displ)
p
p + scale_x_continuous("City mpg")
p + xlab("City mpg")
p + ylab("Highway mpg")
p + labs(x = "City mpg", y = "Highway", colour = "Displacement") +
scale_colour_gradient(breaks = c(2,7))
p + xlab(expression(frac(miles, gallon)))
# The difference between breaks and limits. (Left) default plot with
# {\tt limits = c(4, 8), breaks = 4:8}, (middle) {\tt breaks =
# c(5.5,6.5)} and (right) {\tt limits = c(5.5,6.5)}. The effect on the
# x axis (top) and colour legend (bottom)
p <- qplot(cyl, wt, data = mtcars)
p
p + scale_x_continuous(breaks = c(5.5, 6.5))
p + scale_x_continuous(limits = c(5.5, 6.5))
p <- qplot(wt, cyl, data = mtcars, colour = cyl)
p
p + scale_colour_gradient(breaks = c(5.5, 6.5))
p + scale_colour_gradient(limits = c(5.5, 6.5))
# A scatterplot of diamond price vs.\ carat illustrating the difference
# between log transforming the scale (left) and log transforming the
# data (right). The plots are identical, but the axis labels are
# different.
qplot(log10(carat), log10(price), data = diamonds)
qplot(carat, price, data = diamonds) +
scale_x_log10() + scale_y_log10()
# A time series of personal savings rate. (Left) The default
# appearance, (middle) breaks every 10 years, and (right) scale
# restricted to 2004, with YMD date format. Measurements are recorded
# at the end of each month.
plot <- qplot(date, psavert, data = economics, geom = "line") +
ylab("Personal savings rate") +
geom_hline(xintercept = 0, colour = "grey50")
plot
plot + scale_x_date(breaks = "5 years")
plot + scale_x_date(
limits = as.Date(c("2004-01-01", "2005-01-01"))
)
# Density of eruptions with three colour schemes. (Left) Default
# gradient colour scheme, (middle) customised gradient from white to
# black and (right) 3 point gradient with midpoint set to the mean
# density.
f2d <- with(faithful, MASS::kde2d(eruptions, waiting,
h = c(1, 10), n = 50))
df <- with(f2d, cbind(expand.grid(x, y), as.vector(z)))
names(df) <- c("eruptions", "waiting", "density")
erupt <- ggplot(df, aes(waiting, eruptions, fill = density)) +
geom_tile() +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))
erupt + scale_fill_gradient(limits = c(0, 0.04))
erupt + scale_fill_gradient(limits = c(0, 0.04),
low = "white", high = "black")
erupt + scale_fill_gradient2(limits = c(-0.04, 0.04),
midpoint = mean(df$density))
# Gradient colour scales using perceptually well-formed palettes
# produced by the \code{vcd} package. From left to right: sequential,
# diverging and heat hcl palettes. Each scale is produced with
# \code{scale_fill_gradientn} with \code{colours} set to
# \code{rainbow_hcl(7)}, \code{diverge_hcl(7)} and \code{heat_hcl(7)}.
library(vcd)
fill_gradn <- function(pal) {
scale_fill_gradientn(colours = pal(7), limits = c(0, 0.04))
}
erupt + fill_gradn(rainbow_hcl)
erupt + fill_gradn(diverge_hcl)
erupt + fill_gradn(heat_hcl)
RColorBrewer::display.brewer.all()
# Three ColorBrewer palettes, Set1 (left), Set2 (middle) and Pastel1
# (right), applied to points (top) and bars (bottom). Bright colours
# work well for points, but are overwhelming on bars. Subtle colours
# work well for bars, but are hard to see on points.
point <- qplot(brainwt, bodywt, data = msleep, log = "xy",
colour = vore)
area <- qplot(log10(brainwt), data = msleep, fill = vore,
binwidth = 1)
point + scale_colour_brewer(palette = "Set1")
point + scale_colour_brewer(palette = "Set2")
point + scale_colour_brewer(palette = "Pastel1")
area + scale_fill_brewer(palette = "Set1")
area + scale_fill_brewer(palette = "Set2")
area + scale_fill_brewer(palette = "Pastel1")
# Scale manual used to create custom colour (left and middle) and shape
# (right) scales.
plot <- qplot(brainwt, bodywt, data = msleep, log = "xy")
plot + aes(colour = vore) +
scale_colour_manual(values = c("red", "orange", "yellow",
"green", "blue"))
colours <- c(carni = "red", "NA" = "orange", insecti = "yellow",
herbi = "green", omni = "blue")
#plot + aes(colour = vore) + scale_colour_manual(values = colours)
plot + aes(shape = vore) +
scale_shape_manual(values = c(1, 2, 6, 0, 23))
huron <- data.frame(year = 1875:1972, level = LakeHuron)
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5), colour = "blue") +
geom_line(aes(y = level + 5), colour = "red")
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above"))
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above")) +
scale_colour_manual("Direction", values=c("below" = "blue", "above" = "red"))
require('pprobeData')
date <- row.names(xassetPrices)
assets <- data.frame(date, xassetPrices)
ggplot(assets, aes(as.Date(date))) +
geom_line(aes(y=XA101), colour='red') +
geom_line(aes(y=XA103), colour='blue')
qplot(date, pop, data=economics, geom="line")
qplot(date, pop, data=economics, geom="line", log="y")
qplot(date, pop, data=subset(economics, date > as.Date("2006-1-1")), geom="line")
qplot(date, pop, data=economics, size=unemploy/pop, geom="line")
# A plot of R colours in Luv space. A legend is unnecessary, because
# the colour of the points represents itself: the data and aesthetic
# spaces are the same.
x <- colors()
luv <- as.data.frame(convertColor(t(col2rgb(x)), "sRGB", "Luv"))
qplot(u, v, data=luv, colour = x, size = I(3)) + scale_colour_identity() +
coord_equal()
# Legends produced by geom: point, line, point and line, and bar.
p <- ggplot(diamonds[1:100, ], aes(price, carat, colour = cut)) +
opts(keep = "legend_box")
p + geom_point()
p + geom_line()
p + geom_point() + geom_line()
p + geom_bar(binwidth = 100) + aes(fill = cut, y = ..count..)
# Colour legend, shape legend, colour + shape legend.
p <- ggplot(diamonds[1:100, ], aes(price, carat)) +
geom_point() +
opts(keep = "legend_box")
p + aes(colour = cut)
p + aes(shape = cut)
p + aes(shape = cut, colour = cut)
############# 3d plotting
# 1) static 3d plotting
require('scatterplot3d')
head(iris)
cl <- vector(mode="character", length=nrow(iris))
cl[iris$Species=="setosa"] <- "red"
cl[iris$Species=="versicolor"] <- "green"
cl[iris$Species=="virginica"] <- "blue"
with(iris, scatterplot3d(Sepal.Length, Sepal.Width, Petal.Length,
color=cl, pch=20))
# 2) dynamic 3d plotting
require('rgl')
open3d()
with(iris, plot3d(Sepal.Length, Sepal.Width, Petal.Length,
col=(as.numeric(Species)+1)))
# Chapter 7 Positioning
mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4", "f"))
#qplot(cty, hwy, data = mpg2) + facet_grid(. ~ .)