-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path09-GGPlot.Rmd
168 lines (110 loc) · 7.83 KB
/
09-GGPlot.Rmd
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
# The ggplot world and BrailleR {#GGPlot}
The use of the ggplot style of graph production has increased markedly since its inception with the `ggplot2` package [@Rpkg-ggplot2] now being one of the most used R packages. The "gg" is short for the "grammar of graphics" but the R code used to create an extremely wide range of graphs is seldom human-interpretable with ease. Creation of suitable support functionality via the `VI()` command is very definitely required. An initial attempt to extract any useful infromation from these graphs was contributed to the `BrailleR` package by Tony Hirst. Significant improvement has been made by Debra Warren as part of her postgraduate work under the supervision of Paul Murrell at the University of Auckland. Much of what is displayed in this chapter is only worth offering because of Debra's work and the interactions had between her, Paul and I in the second half of 2017.
In 2021, the `plot.ggplot()` command in the `ggplot2` package was updated to automatically call `BrailleR`'s `VI()` function if the user has specified they are blind. The `GoBlind()` and `GoSighted()` commands will toggle the automated use of `VI()` on and off respectively. The default starting mode is to get the automated `VI()` output.
The search was then on for someone to come on board and start ramping up the number of different graph types that `VI()` can handle. James Thompson was a summer student who climbed into the task in November 2022. One of his first questions was to ask me when `BrailleR` would take the bold step into version 1.x.y, and maybe his efforts will get us close.
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(fig.width=7, fig.height=5, comment="")
library(BrailleR)
library(ggplot2)
library(magrittr)
```
N.B. the commands here are minor alterations to the commands presented in @Wickham2009ggplot2; any major changes will be explicitly noted, but do note the use of the native R pipe `|>` instead of the older `%>%` pipe. All plots are created using the figure numbers from @Wickham2009ggplot2 or the page numbers if no figure number was given. They are then automatically investigated using the `VI()` command from the `BrailleR` package which has not needed to be explicitly called since v0.32.0 of `BrailleR` and v3.3.5 of the `ggplot2` package.
You will need some additional packages to the `BrailleR` package to be loaded to follow along with the examples in this chapter. Do this by issuing the commands:
```{r GetLibraries}
library(BrailleR)
library(ggplot2)
```
Note that one data set used for these examples is created by @Wickham2009ggplot2 while the others are included in the `ggplot2` package.
```{r makeData}
set.seed(1410)
dsmall <- diamonds[sample(nrow(diamonds), 100),]
```
One important note for the graphs in this chapter is the difference in the way they appear here, as compared to the original figures of @Wickham2009ggplot2 where differing height and width parameters have been set for each graph. For example, in the following graph, the points are smaller than in the original figure, and the aspect ratio is slightly different. The consequence is that this graph looks less cluttered than does the original.
```{r p11a, fig.cap="First graph on page 11 of @Wickham2009ggplot2"}
p11a = qplot(carat, price, data = diamonds)
p11a
```
Note that if you do not want to generate the graph but want to know what its text description has to offer, you can use the `|>` pipe operator as follows:
```{r fig2-2a, fig.cap="Left pane of Figure 2.2 of @Wickham2009ggplot2"}
fig2.2a = qplot(carat, price, data = dsmall, colour = color)
fig2.2a |> VI()
```
N.B. It is not necessary to keep the graphs for `BrailleR` to do its magic, so from here onwards, they will not be explicitly stored and called back as was done above.
We haven't been able to tell what exact colour was used in the @Wickham2009ggplot2 rendering of this graph, but there has obviously been some minor alteration of the colour palette being used by the `ggplot2` package over the years.
```{r fig2-2b, fig.cap="Right pane of Figure 2.2"}
qplot(carat, price, data = dsmall, shape = cut)
```
To get semi-transparent points:
```{r fig2-3b, fig.cap="Middle pane from Figure 2.3"}
qplot(carat, price, data = diamonds, alpha = I(1/100))
```
To add a smoother (default is loess for n<1000):
```{r fig2-4a, fig.cap="Left pane of Figure 2.4"}
qplot(carat, price, data = dsmall, geom = c("point", "smooth"))
```
## Plotting a continuous variable against a categorical variable
```{r fig2-8a, fig.cap="Left pane of Figure 2.8"}
qplot(color, price / carat, data = diamonds, geom = "jitter")
```
```{r fig2-8b}
qplot(color, price / carat, data = diamonds, geom = "boxplot")
```
When seeking to use shading or opaqueness to describe the density of the points, the fact the size of the points has an impact on the opaqueness is not currently realised by `BrailleR`.
```{r fig2-9b, fig.cap="Middle pane of Figure 2.9"}
qplot(color, price / carat, data = diamonds, geom = "jitter", alpha = I(1 / 50))
```
### Univariate plots
```{r fig2-10a, fig.cap="Left pane of Figure 2.10"}
qplot(carat, data = diamonds, geom = "histogram")
```
Warning: This figure does look different to the original in @Wickham2009ggplot2 ins spite of using the same code and same data.
```{r fig2-10b, fig.cap="Right pane of Figure 2.10"}
qplot(carat, data = diamonds, geom = "density")
```
```{r fig2-11c, fig.cap="Right pane of Figure 2.11"}
qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.01, xlim = c(0,3))
```
The data is separated by implication in the following graphs. The legend is automatically generated and has altered in appearance since the original was produced in @Wickham2009ggplot2.
```{r fig2-12a, fig.cap="Left pane of Figure 2.12"}
qplot(carat, data = diamonds, geom = "density", colour = color)
```
```{r fig2-12b, fig.cap="Right pane of Figure 2.12"}
qplot(carat, data = diamonds, geom = "histogram", fill = color)
```
### Bar charts for categorical variables
```{r fig2-13a, fig.cap="Left pane of Figure 2.13"}
qplot(color, data = diamonds, geom = "bar") #geom="bar" is the default
```
need to check...
```{r fig2-13b, fig.cap="Right pane of Figure 2.13"}
qplot(color, data = diamonds, geom = "bar", weight = carat)
qplot(color, data = diamonds, geom = "bar", weight = carat) + scale_y_continuous("carat")
```
## Time series plots
It looks like the data used in the next graph has been updated since the publication of @Wickham2009ggplot2
```{r fig2-14a, fig.cap="Left pane of Figure 2.14"}
qplot(date, unemploy / pop, data = economics, geom = "line")
```
## Path plots
```{r fig2-15b, fig.cap="Right pane of Figure 2.15"}
year <- function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = economics, geom = "path", colour=year(date))
#+ scale_area() # no longer works
```
## Facets is the ggplot term for trellis' panels
The aspect ratio for the plot region is something that needs to be considered. I've manually adjusted the plotting window here so that the graph more closely matches that of @Wickham2009ggplot2 but it is not an exact match.
```{r fig2-16a, fig.cap="Left side of Figure 2.16", fig.height=10, fig.width=4}
qplot(carat, data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1,
xlim = c(0, 3))
```
```{r fig2-16b, fig.cap="Right side of Figure 2.16", fig.height=10, fig.width=4}
qplot(carat, ..density.., data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1, xlim = c(0, 3))
```
## Rescaling of the axes
```{r p26a, fig.cap="First graph on page 26 of "}
qplot(carat, price, data = dsmall, log = "xy")
```
```{r fig3-6, fig.cap="Figure 3.6 of "}
qplot(displ, hwy, data=mpg, facets =~ year) + geom_smooth()
```