-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathREADME.Rmd
242 lines (190 loc) · 8.13 KB
/
README.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
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README/",
cache.path = "README/cache/",
cache = TRUE,
autodep=TRUE,
cache.rebuild = TRUE
)
```
# Lemon --- Freshing up your ggplots
[![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/lemon)](https://cran.r-project.org/package=lemon)
![downloads](http://cranlogs.r-pkg.org/badges/grand-total/lemon)
<!-- badges: start -->
[![R-CMD-check](https://github.com/stefanedwards/lemon/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/stefanedwards/lemon/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
Just another [ggplot2](http://ggplot2.tidyverse.org) and
[knitr](https://yihui.name/knitr/) extension package.
This package contains functions primarily in these domains of ggplot2:
```{r domain_axis_lines,include=FALSE,fig.height=0.5,fig.width=0.5}
library(ggplot2)
library(lemon)
ggplot(mtcars, aes(x=cyl, y=mpg)) +
geom_point(size=0.1) +
coord_capped_cart(bottom=brackets_horisontal(), left='both') +
theme_light() +
theme(panel.border=element_blank(), axis.line = element_line(),
axis.title=element_blank(), axis.text=element_blank(),
panel.grid=element_blank(), axis.ticks.x = element_line(colour='black'))
```
```{r domain_facets,include=FALSE,fig.height=0.5, fig.width=0.5}
ggplot(mtcars, aes(x=cyl, y=mpg)) +
geom_point(size=0.1) +
coord_capped_cart(bottom='both', left='both') +
facet_rep_wrap(~carb) +
theme_light() +
theme(panel.border=element_blank(), axis.line = element_line(),
axis.title=element_blank(), axis.text=element_blank(),
panel.grid=element_blank(), axis.ticks = element_blank(),
axis.ticks.length = unit(0, 'npc'), panel.spacing=unit(1, 'mm'),
strip.background=element_blank(), strip.text=element_blank())
```
```{r domain_pointline,include=FALSE,fig.height=0.5,fig.width=0.5}
ggplot(data.frame(x=1:3, y=c(1,2.4,3.5)), aes(x, y)) +
geom_pointline(size=0.5) +
coord_capped_cart(xlim=c(0,4), bottom='both', left='both') +
theme_light() +
theme(panel.border=element_blank(), axis.line = element_line(),
axis.title=element_blank(), axis.text=element_blank(),
panel.grid=element_blank(), axis.ticks.x = element_line(colour='black'))
```
- ![Axis lines miniature](README/domain_axis_lines-1.png) Axis lines.
- ![Facets miniature](README/domain_facets-1.png) Repeated axis lines on facets.
- ![geom_pointline](README/domain_pointline-1.png) `geom_pointpath` and `geom_pointline`.
- Legends
As well as some functions in knitr.
## Installation
```{r, eval = FALSE}
# install.packages("devtools")
# Install release from GitHub:
devtools::install_github("stefanedwards/lemon", ref='v0.4.7')
# Or get the lastest development version from GitHub:
devtools::install_github("stefanedwards/lemon")
```
## Axis lines
We can display a limit on the axes range.
```{r usage1}
library(lemon)
ggplot(mtcars, aes(x=cyl, y=mpg)) +
geom_point() +
coord_capped_cart(bottom='both', left='none') +
theme_light() + theme(panel.border=element_blank(), axis.line = element_line())
```
**NB**: Disable `panel.border` and enable `axis.line` in `theme`, otherwise you will not see an effect!
We could also show that the x-axis is categorical (or ordinal):
```{r brackets_demo}
(p <- ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) +
geom_point(position=position_jitter(width=0.1)) +
coord_flex_cart(bottom=brackets_horisontal(), left=capped_vertical('both')) +
theme_light() + theme(panel.border=element_blank(), axis.line = element_line())
)
```
When capping the axis lines, they are never capped further inwards than the ticks!
Look up
* `coord_capped_cart`, `coord_capped_flip`
* `coord_flex_cart`, `coord_flex_flip`, `coord_flex_fixed`
* `brackets_horisontal`, `brackets_vertical`
* `capped_horisontal`, `capped_vertical`
## Facets
Having produced such wonderous axes, it is a pity they are not plotted around
all panels when using faceting.
We have extended both `facet_grid` and `facet_wrap` to produce axis, ticks, and
labels on _all_ panels:
```{r facets}
p + facet_rep_wrap(~gear, ncol=2, label=label_both)
```
They work just like the normal ones; look up `facet_rep_grid` and `facet_rep_wrap`.
## `geom_pointline`
A geom that combines both points and lines.
While possible by using both `geom_point` and `geom_line`, position adjustments
are not preserved between the two layers.
`geom_pointline` and `geom_pointpath` combines `geom_point` with
`geom_line` and `geom_path`, respectively, while preserving position adjustments.
```{r geom_pointline_demo,echo=FALSE}
library(grid)
library(gtable)
p <- ggplot(mtcars, aes(wt, mpg, colour=factor(cyl))) +
geom_point(col='grey') +
theme_light() + theme(panel.border=element_blank(), axis.line = element_line()) +
theme(legend.position='hidden', plot.title = element_text(size=rel(0.8)))
p1 <- p + geom_point(position=position_jitter(0.1, 0.1)) + geom_line(position=position_jitter(0.1, 0.1)) +
labs(title='geom_point(position=position_jitter(0.1, 0.1)) + \ngeom_line(position=position_jitter(0.1, 0.1))')
p2 <- p + geom_pointline(position=position_jitter(0.1, 0.1)) +
labs(title='geom_pointline(position=position_jitter(0.1, 0.1))')
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
grid.newpage()
grid.draw(cbind(g1, g2, size = 'first'))
```
Left: `geom_point` and `geom_line` as two separate geoms.
Right: The two geoms combined into `geom_pointline`.
Both produced with `ggplot(mtcars, aes(wt, mpg, colour=factor(cyl))) + geom_point(col='grey')`, where the grey points indicate the true location of the datapoint.
An added visual effect is seen as the lines do not touch the points, leaving a
small gap (set by argument `distance`).
## Legends
Reposition the legend onto the plot. Exactly where you want it:
```{r reposition_legend}
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
d <- ggplot(dsamp, aes(carat, price)) +
geom_point(aes(colour = clarity))
reposition_legend(d, 'top left')
```
The legend repositioned onto the top left corner of the panel.
Scavenging the Internet, we have found some functions that help work with
legends.
Frequently appearing on [Stack Overflow](https://stackoverflow.com), we bring
you `g_legend`:
```{r g_legend,fig.height=3,fig.width=2}
library(grid)
legend <- g_legend(d)
grid.newpage()
grid.draw(legend)
```
The legend grob, by itself.
Originally brought to you by
(Baptiste Auguié)[http://baptiste.github.io/]
(<https://github.com/tidyverse/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs>)
and
(Shaun Jackman)[http://rpubs.com/sjackman]
(<http://rpubs.com/sjackman/grid_arrange_shared_legend>).
We put it in a package.
```{r grid_arrange_shared_legend}
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data = dsamp, colour = clarity)
p2 <- qplot(cut, price, data = dsamp, colour = clarity)
p3 <- qplot(color, price, data = dsamp, colour = clarity)
p4 <- qplot(depth, price, data = dsamp, colour = clarity)
grid_arrange_shared_legend(p1, p2, p3, p4, ncol = 2, nrow = 2)
```
Four plots that share the same legend.
## Extensions to knitr
`knitr` allows S3 methods for `knit_print` for specialised printing of objects.
We provide `lemon_print` for data frames, dplyr tables, and summary objects,
that can be used to render the output, without mucking up the code source.
An added benefit is that we can use RStudio's inline data frame viewer:
![Viewing data frames in R Notebooks in RStudio](vignettes/lemon_print_capture.png)
### Relative file paths made safe
Using `knitr` for computations that use external binaries and/or write temporary
files, setting the root directory for `knitr`'s knitting saves the user from a
file mess. E.g.
```r
knitr::opts_knit$set(root.dir=TMPDIR)
```
But we want to keep our file paths relative for the scripts / document to be
transferable. We introduce the `.dot` functions:
```r
TMPDIR=tempdir()
.data <- .dot('data')
knitr_opts_knit$set(root.dir=TMPDIR)
```
We can then load our data file using the created `.data` function,
even though the chunk is executed from TMPDIR.
```r
dat <- read.table(.data('mydata.tab'))
```