graphics examples

Based on examples in R Graphics Cookbook

See also: https://www.rstudio.com/wp-content/uploads/2015/12/ggplot2-cheatsheet-2.0.pdf

#install.packages(c("ggplot2","gcookbook"))
library(ggplot2)
library(gcookbook)

data(package="gcookbook")
data(package="MASS")

vignette(package="ggplot2")
vignette(package="gcookbook")
## no vignettes found

Creating a Histogram

Use histograms to view the distribution of one-dimensional data

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
#View(mtcars)
ggplot(mtcars, aes(x=mpg)) + geom_histogram(binwidth=4)

Creating a Box Plot

box plots compare distributions pass in: factor of x values, and vector of y values

#View(ToothGrowth)

ggplot(ToothGrowth, aes(x=supp, y=len)) + geom_boxplot()

Making a Basic Scatter Plot

Use geom_point(), and map one variable to x and one to y.

default shape: #16 - solid circles default size: 2

str(heightweight)
## 'data.frame':    236 obs. of  5 variables:
##  $ sex     : Factor w/ 2 levels "f","m": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ageYear : num  11.9 12.9 12.8 13.4 15.9 ...
##  $ ageMonth: int  143 155 153 161 191 171 185 142 160 140 ...
##  $ heightIn: num  56.3 62.3 63.3 59 62.5 62.5 59 56.5 62 53.8 ...
##  $ weightLb: num  85 105 108 92 112 ...
heightweight[,c("ageYear","heightIn")]
##     ageYear heightIn
## 1     11.92     56.3
## 2     12.92     62.3
## 3     12.75     63.3
## 4     13.42     59.0
## 5     15.92     62.5
## 6     14.25     62.5
## 7     15.42     59.0
## 8     11.83     56.5
## 9     13.33     62.0
## 10    11.67     53.8
## 11    11.58     61.5
## 12    14.83     61.5
## 13    13.08     64.5
## 14    12.42     58.3
## 15    11.92     51.3
## 16    12.08     58.8
## 17    15.92     65.3
## 18    12.50     59.5
## 19    12.25     61.3
## 20    15.00     63.3
## 21    11.75     61.8
## 22    11.67     53.5
## 23    13.67     58.0
## 24    14.67     61.3
## 25    15.42     63.3
## 26    13.83     61.5
## 27    14.58     60.8
## 28    15.00     59.0
## 29    17.50     65.5
## 30    12.17     56.3
## 31    14.17     64.3
## 32    13.50     58.0
## 33    12.42     64.3
## 34    11.58     57.5
## 35    15.50     57.8
## 36    16.42     61.5
## 37    14.08     62.3
## 38    14.75     61.8
## 39    15.42     65.3
## 40    15.17     58.3
## 41    14.42     62.8
## 42    13.83     59.3
## 43    14.00     61.5
## 44    14.08     62.0
## 45    12.50     61.3
## 46    15.33     62.3
## 47    11.58     52.8
## 48    12.25     59.8
## 49    12.00     59.5
## 50    14.75     61.3
## 51    14.83     63.5
## 52    16.42     64.8
## 53    12.17     60.0
## 54    12.08     59.0
## 55    12.25     55.8
## 56    12.08     57.8
## 57    12.92     61.3
## 58    13.92     62.3
## 59    15.25     64.3
## 60    11.92     55.5
## 61    15.25     64.5
## 62    15.42     60.0
## 63    12.33     56.3
## 64    12.25     58.3
## 65    12.83     60.0
## 66    13.00     54.5
## 67    12.00     55.8
## 68    12.83     62.8
## 69    12.67     60.5
## 70    15.92     63.3
## 71    15.83     66.8
## 72    11.67     60.0
## 73    12.33     60.5
## 74    15.75     64.3
## 75    11.92     58.3
## 76    14.83     66.5
## 77    13.67     65.3
## 78    13.08     60.5
## 79    12.25     59.5
## 80    12.33     59.0
## 81    14.75     61.3
## 82    14.25     61.5
## 83    14.33     64.8
## 84    15.83     56.8
## 85    15.25     66.5
## 86    11.92     61.5
## 87    14.92     63.0
## 88    15.50     57.0
## 89    15.17     65.5
## 90    15.17     62.0
## 91    11.83     56.0
## 92    13.75     61.3
## 93    13.75     55.5
## 94    12.83     61.0
## 95    12.50     54.5
## 96    12.92     66.0
## 97    13.58     56.5
## 98    11.75     56.0
## 99    12.25     51.5
## 100   17.50     62.0
## 101   14.25     63.0
## 102   13.92     61.0
## 103   15.17     64.0
## 104   12.00     61.0
## 105   16.08     59.8
## 106   11.75     61.3
## 107   13.67     63.3
## 108   15.50     63.5
## 109   14.08     61.5
## 110   14.58     60.3
## 111   15.00     61.3
## 112   13.75     64.8
## 113   13.08     60.5
## 114   12.00     57.3
## 115   12.50     59.5
## 116   12.50     60.8
## 117   11.58     60.5
## 118   15.75     67.0
## 119   15.25     64.8
## 120   12.25     50.5
## 121   12.17     57.5
## 122   13.33     60.5
## 123   13.00     61.8
## 124   14.42     61.3
## 125   12.58     66.3
## 126   11.75     53.3
## 127   12.50     59.0
## 128   13.67     57.8
## 129   12.75     60.0
## 130   17.17     68.3
## 132   14.67     63.8
## 133   14.67     65.0
## 134   11.67     59.5
## 135   15.42     66.0
## 136   15.00     61.8
## 137   12.17     57.3
## 138   15.25     66.0
## 139   11.67     56.5
## 140   12.58     58.3
## 141   12.58     61.0
## 142   12.00     62.8
## 143   13.33     59.3
## 144   14.83     67.3
## 145   16.08     66.3
## 146   13.50     64.5
## 147   13.67     60.5
## 148   15.50     66.0
## 149   11.92     57.5
## 150   14.58     64.0
## 151   14.58     68.0
## 152   14.58     63.5
## 153   14.42     69.0
## 154   14.17     63.8
## 155   14.50     66.0
## 156   13.67     63.5
## 157   12.00     59.5
## 158   13.00     66.3
## 159   12.42     57.0
## 160   12.00     60.0
## 161   12.25     57.0
## 162   15.67     67.3
## 163   14.08     62.0
## 164   14.33     65.0
## 165   12.50     59.5
## 166   16.08     67.8
## 167   13.08     58.0
## 168   14.00     60.0
## 169   11.67     58.5
## 170   13.00     58.3
## 171   13.00     61.5
## 172   13.17     65.0
## 173   15.33     66.5
## 174   13.00     68.5
## 175   12.00     57.0
## 176   14.67     61.5
## 177   14.00     66.5
## 178   12.42     52.5
## 179   11.83     55.0
## 180   15.67     71.0
## 181   16.92     66.5
## 182   11.83     58.8
## 183   15.75     66.3
## 184   15.67     65.8
## 185   16.67     71.0
## 186   12.67     59.5
## 187   14.50     69.8
## 188   13.83     62.5
## 189   12.08     56.5
## 190   11.92     57.5
## 191   13.58     65.3
## 192   13.83     67.3
## 193   15.17     67.0
## 194   14.42     66.0
## 195   12.92     61.8
## 196   13.50     60.0
## 197   14.75     63.0
## 198   14.75     60.5
## 199   14.58     65.5
## 200   13.83     62.0
## 201   12.50     59.0
## 202   12.50     61.8
## 203   15.67     63.3
## 204   13.58     66.0
## 205   14.25     61.8
## 206   13.50     63.0
## 207   11.75     57.5
## 208   14.50     63.0
## 209   11.83     56.0
## 210   12.33     60.5
## 211   11.67     56.8
## 212   13.33     64.0
## 213   12.00     60.0
## 214   17.17     69.5
## 215   13.25     63.3
## 216   12.42     56.3
## 217   16.08     72.0
## 218   16.17     65.3
## 219   12.67     60.8
## 220   12.17     55.0
## 221   11.58     55.0
## 222   15.50     66.5
## 223   13.42     56.8
## 224   12.75     64.8
## 225   16.33     64.5
## 226   13.67     58.0
## 227   13.25     62.8
## 228   14.83     63.8
## 229   12.75     57.8
## 230   12.92     57.3
## 231   14.83     63.5
## 232   11.83     55.0
## 233   13.67     66.5
## 234   15.75     65.0
## 235   13.67     61.5
## 236   13.92     62.0
## 237   12.58     59.3
ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()

Group Data Points by a Variable Using Shape or Color

map the grouping variable to shape or colour grouping variable must be categorical (factor or character vector)

heightweight[,c("sex","ageYear","heightIn")]
##     sex ageYear heightIn
## 1     f   11.92     56.3
## 2     f   12.92     62.3
## 3     f   12.75     63.3
## 4     f   13.42     59.0
## 5     f   15.92     62.5
## 6     f   14.25     62.5
## 7     f   15.42     59.0
## 8     f   11.83     56.5
## 9     f   13.33     62.0
## 10    f   11.67     53.8
## 11    f   11.58     61.5
## 12    f   14.83     61.5
## 13    f   13.08     64.5
## 14    f   12.42     58.3
## 15    f   11.92     51.3
## 16    f   12.08     58.8
## 17    f   15.92     65.3
## 18    f   12.50     59.5
## 19    f   12.25     61.3
## 20    f   15.00     63.3
## 21    f   11.75     61.8
## 22    f   11.67     53.5
## 23    f   13.67     58.0
## 24    f   14.67     61.3
## 25    f   15.42     63.3
## 26    f   13.83     61.5
## 27    f   14.58     60.8
## 28    f   15.00     59.0
## 29    f   17.50     65.5
## 30    f   12.17     56.3
## 31    f   14.17     64.3
## 32    f   13.50     58.0
## 33    f   12.42     64.3
## 34    f   11.58     57.5
## 35    f   15.50     57.8
## 36    f   16.42     61.5
## 37    f   14.08     62.3
## 38    f   14.75     61.8
## 39    f   15.42     65.3
## 40    f   15.17     58.3
## 41    f   14.42     62.8
## 42    f   13.83     59.3
## 43    f   14.00     61.5
## 44    f   14.08     62.0
## 45    f   12.50     61.3
## 46    f   15.33     62.3
## 47    f   11.58     52.8
## 48    f   12.25     59.8
## 49    f   12.00     59.5
## 50    f   14.75     61.3
## 51    f   14.83     63.5
## 52    f   16.42     64.8
## 53    f   12.17     60.0
## 54    f   12.08     59.0
## 55    f   12.25     55.8
## 56    f   12.08     57.8
## 57    f   12.92     61.3
## 58    f   13.92     62.3
## 59    f   15.25     64.3
## 60    f   11.92     55.5
## 61    f   15.25     64.5
## 62    f   15.42     60.0
## 63    f   12.33     56.3
## 64    f   12.25     58.3
## 65    f   12.83     60.0
## 66    f   13.00     54.5
## 67    f   12.00     55.8
## 68    f   12.83     62.8
## 69    f   12.67     60.5
## 70    f   15.92     63.3
## 71    f   15.83     66.8
## 72    f   11.67     60.0
## 73    f   12.33     60.5
## 74    f   15.75     64.3
## 75    f   11.92     58.3
## 76    f   14.83     66.5
## 77    f   13.67     65.3
## 78    f   13.08     60.5
## 79    f   12.25     59.5
## 80    f   12.33     59.0
## 81    f   14.75     61.3
## 82    f   14.25     61.5
## 83    f   14.33     64.8
## 84    f   15.83     56.8
## 85    f   15.25     66.5
## 86    f   11.92     61.5
## 87    f   14.92     63.0
## 88    f   15.50     57.0
## 89    f   15.17     65.5
## 90    f   15.17     62.0
## 91    f   11.83     56.0
## 92    f   13.75     61.3
## 93    f   13.75     55.5
## 94    f   12.83     61.0
## 95    f   12.50     54.5
## 96    f   12.92     66.0
## 97    f   13.58     56.5
## 98    f   11.75     56.0
## 99    f   12.25     51.5
## 100   f   17.50     62.0
## 101   f   14.25     63.0
## 102   f   13.92     61.0
## 103   f   15.17     64.0
## 104   f   12.00     61.0
## 105   f   16.08     59.8
## 106   f   11.75     61.3
## 107   f   13.67     63.3
## 108   f   15.50     63.5
## 109   f   14.08     61.5
## 110   f   14.58     60.3
## 111   f   15.00     61.3
## 112   m   13.75     64.8
## 113   m   13.08     60.5
## 114   m   12.00     57.3
## 115   m   12.50     59.5
## 116   m   12.50     60.8
## 117   m   11.58     60.5
## 118   m   15.75     67.0
## 119   m   15.25     64.8
## 120   m   12.25     50.5
## 121   m   12.17     57.5
## 122   m   13.33     60.5
## 123   m   13.00     61.8
## 124   m   14.42     61.3
## 125   m   12.58     66.3
## 126   m   11.75     53.3
## 127   m   12.50     59.0
## 128   m   13.67     57.8
## 129   m   12.75     60.0
## 130   m   17.17     68.3
## 132   m   14.67     63.8
## 133   m   14.67     65.0
## 134   m   11.67     59.5
## 135   m   15.42     66.0
## 136   m   15.00     61.8
## 137   m   12.17     57.3
## 138   m   15.25     66.0
## 139   m   11.67     56.5
## 140   m   12.58     58.3
## 141   m   12.58     61.0
## 142   m   12.00     62.8
## 143   m   13.33     59.3
## 144   m   14.83     67.3
## 145   m   16.08     66.3
## 146   m   13.50     64.5
## 147   m   13.67     60.5
## 148   m   15.50     66.0
## 149   m   11.92     57.5
## 150   m   14.58     64.0
## 151   m   14.58     68.0
## 152   m   14.58     63.5
## 153   m   14.42     69.0
## 154   m   14.17     63.8
## 155   m   14.50     66.0
## 156   m   13.67     63.5
## 157   m   12.00     59.5
## 158   m   13.00     66.3
## 159   m   12.42     57.0
## 160   m   12.00     60.0
## 161   m   12.25     57.0
## 162   m   15.67     67.3
## 163   m   14.08     62.0
## 164   m   14.33     65.0
## 165   m   12.50     59.5
## 166   m   16.08     67.8
## 167   m   13.08     58.0
## 168   m   14.00     60.0
## 169   m   11.67     58.5
## 170   m   13.00     58.3
## 171   m   13.00     61.5
## 172   m   13.17     65.0
## 173   m   15.33     66.5
## 174   m   13.00     68.5
## 175   m   12.00     57.0
## 176   m   14.67     61.5
## 177   m   14.00     66.5
## 178   m   12.42     52.5
## 179   m   11.83     55.0
## 180   m   15.67     71.0
## 181   m   16.92     66.5
## 182   m   11.83     58.8
## 183   m   15.75     66.3
## 184   m   15.67     65.8
## 185   m   16.67     71.0
## 186   m   12.67     59.5
## 187   m   14.50     69.8
## 188   m   13.83     62.5
## 189   m   12.08     56.5
## 190   m   11.92     57.5
## 191   m   13.58     65.3
## 192   m   13.83     67.3
## 193   m   15.17     67.0
## 194   m   14.42     66.0
## 195   m   12.92     61.8
## 196   m   13.50     60.0
## 197   m   14.75     63.0
## 198   m   14.75     60.5
## 199   m   14.58     65.5
## 200   m   13.83     62.0
## 201   m   12.50     59.0
## 202   m   12.50     61.8
## 203   m   15.67     63.3
## 204   m   13.58     66.0
## 205   m   14.25     61.8
## 206   m   13.50     63.0
## 207   m   11.75     57.5
## 208   m   14.50     63.0
## 209   m   11.83     56.0
## 210   m   12.33     60.5
## 211   m   11.67     56.8
## 212   m   13.33     64.0
## 213   m   12.00     60.0
## 214   m   17.17     69.5
## 215   m   13.25     63.3
## 216   m   12.42     56.3
## 217   m   16.08     72.0
## 218   m   16.17     65.3
## 219   m   12.67     60.8
## 220   m   12.17     55.0
## 221   m   11.58     55.0
## 222   m   15.50     66.5
## 223   m   13.42     56.8
## 224   m   12.75     64.8
## 225   m   16.33     64.5
## 226   m   13.67     58.0
## 227   m   13.25     62.8
## 228   m   14.83     63.8
## 229   m   12.75     57.8
## 230   m   12.92     57.3
## 231   m   14.83     63.5
## 232   m   11.83     55.0
## 233   m   13.67     66.5
## 234   m   15.75     65.0
## 235   m   13.67     61.5
## 236   m   13.92     62.0
## 237   m   12.58     59.3
ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex )) + geom_point()

ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex )) + geom_point()

ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex )) + geom_point()

ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex )) +
  geom_point() +
  scale_shape_manual(values=c(1,2)) +
  scale_colour_brewer(palette="Set1")

facets

p <- ggplot(heightweight, aes(x=ageYear, y=heightIn )) + geom_point()

p + facet_grid( sex ~ .)

mpg dataset (included with ggplot2 package)

dim(mpg)
## [1] 234  11
str(mpg)
## Classes 'tbl_df', 'tbl' and 'data.frame':    234 obs. of  11 variables:
##  $ manufacturer: chr  "audi" "audi" "audi" "audi" ...
##  $ model       : chr  "a4" "a4" "a4" "a4" ...
##  $ displ       : num  1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
##  $ year        : int  1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
##  $ cyl         : int  4 4 4 4 6 6 6 4 4 4 ...
##  $ trans       : chr  "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
##  $ drv         : chr  "f" "f" "f" "f" ...
##  $ cty         : int  18 21 20 21 16 18 18 18 16 20 ...
##  $ hwy         : int  29 29 31 30 26 26 27 26 25 28 ...
##  $ fl          : chr  "p" "p" "p" "p" ...
##  $ class       : chr  "compact" "compact" "compact" "compact" ...
#View(mpg)

mtcars dataset (included with base R datasets)

https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html

dim(mtcars)
## [1] 32 11
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
#View(mtcars)

diamonds dataset (included with ggplot2 package)

dim(diamonds)
## [1] 53940    10
str(diamonds)
## Classes 'tbl_df', 'tbl' and 'data.frame':    53940 obs. of  10 variables:
##  $ carat  : num  0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##  $ cut    : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
##  $ color  : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
##  $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
##  $ depth  : num  61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##  $ table  : num  55 61 65 58 58 57 57 55 61 61 ...
##  $ price  : int  326 326 327 334 335 336 336 337 337 338 ...
##  $ x      : num  3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
##  $ y      : num  3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
##  $ z      : num  2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
#View(diamonds)

Making a Cleveland dot plot

See also: https://www.perceptualedge.com/articles/b-eye/dot_plots.pdf

tophit <- tophitters2001[1:25,c("name","lg","avg")]
ggplot(tophit,aes(x=avg,y=name)) + geom_point()

ggplot(tophit,aes(x=avg,y=reorder(name,avg))) + geom_point()

ggplot(tophit,aes(x=avg,y=reorder(name,avg))) + geom_point(size=3) +
  theme_bw() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour="grey60", linetype = "dashed"))

ggplot(tophit,aes(x=reorder(name,avg), y=avg)) + geom_point(size=3) +
  theme_bw() +
  theme(axis.text.x = element_text(angle=60, hjust=1),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x = element_line(colour="grey60", linetype = "dashed"))

nameorder <- tophit$name[order(tophit$lg, tophit$avg)]

# turn name into a factor, with levels in the order of nameorder
tophit$name <- factor(tophit$name, levels=nameorder)

ggplot(tophit, aes(x=avg, y=name)) +
    geom_segment(aes(yend=name), xend=0, colour="grey50") +
    geom_point(size=3, aes(colour=lg)) +
    scale_colour_brewer(palette="Set1", limits=c("NL","AL")) +
    theme_bw() +
    theme(panel.grid.major.y = element_blank(),   # No horizontal grid lines
          legend.position=c(1, 0.55),             # Put legend inside plot area
          legend.justification=c(1, 0.5))

# using facets
ggplot(tophit, aes(x=avg, y=name)) +
    geom_segment(aes(yend=name), xend=0, colour="grey50") +
    geom_point(size=3, aes(colour=lg)) +
    scale_colour_brewer(palette="Set1", limits=c("NL","AL"), guide=FALSE) +
    theme_bw() +
    theme(panel.grid.major.y = element_blank()) +
    facet_grid(lg ~ ., scales="free_y", space="free_y")