-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
105 lines (76 loc) · 3.48 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
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
# ggpointgrid
This package provides geoms to rearrange scatter-plot coordinates on regular grids while strictly avoiding over-plotting. The applications are similar to `geom_jitter`.
### Installation
You can install the development version from github with the following command (in your R console):
```
if(!require('remotes')) install.packages('remotes')
remotes::install_github("nevrome/ggpointgrid")
```
### Examples
```{r}
library(ggplot2)
set.seed(5)
```
#### `geom_pointgrid`
`geom_pointgrid` aims to optimize the arrangement of observations on a regular grid. This works well for figures with continuously scaled x- and y-axes, so for scatter-plots or even map plots. Just as in `geom_jitter` the rearrangement of the points reduces accuracy and precision of positional information on x and y in favour of making **every** observation visible.
The grid properties are controlled with the parameters `grid_x` and `grid_y`, which allow to precisely specify the desired graticules.
```{r, warning=FALSE}
df <- tibble::tibble(
x = rep(c(1,1,2,3,3), times = 10),
y = rep(c(1,3,2,1,3), times = 10),
var = sample(c("A", "B", "C"), size = 50, replace = T)
)
coord <- coord_fixed(xlim = c(0.5,3.5), ylim = c(0.5,3.5))
p1 <- ggplot(df) +
geom_point(aes(x, y, color = var)) +
coord + ggtitle("geom_point")
p2 <- ggplot(df) +
geom_jitter(aes(x, y, color = var), width = 0.3, height = 0.3) +
coord + ggtitle("geom_jitter")
p3 <- ggplot(df) +
ggpointgrid::geom_pointgrid(aes(x, y, color = var), grid_x = 15, grid_y = 15) +
coord + ggtitle("geom_pointgrid")
p4 <- ggplot(df) +
ggpointgrid::geom_pointgrid(
aes(x, y, color = var),
grid_x = seq(min(df$x) - 0.3, max(df$x) + 0.3, length.out = 18),
grid_y = seq(min(df$y) - 0.3, max(df$y) + 0.3, length.out = 18)
) +
coord + ggtitle("geom_pointgrid, grid specified")
cowplot::plot_grid(p1, p2, p3, p4)
```
`geom_textgrid` performs the same arrangement operation on text data. It is to `geom_text` what `geom_pointgrid` is to `geom_point`.
#### `geom_pointrect`
`geom_pointrect` was designed for a slightly different use-case than `geom_pointgrid`. Here all observations that share the x- and y-coordinate are spread out into a rectangular grid, representing only this one position. This is especially useful, when the x- and y- axis are ordinally scaled.
The order within each rectangle can be set by the order of the input data.frame and the arguments `scale_x` and `scale_y` control the size of the per-position box. The arguments `round_x` and `round_y` allow to specify how data on continuously scaled x- and y-axes should be aggregated.
```{r, warning=FALSE}
df <- tibble::tibble(
x = rep(letters[c(1,1,2,3,3)], times = 10),
y = rep(letters[c(1,3,2,1,3)], times = 10),
var = sample(c("A", "B", "C"), size = 50, replace = T)
) |> dplyr::arrange(var)
coord <- coord_fixed()
p4 <- ggplot(df) +
geom_point(aes(x, y, color = var)) +
coord + ggtitle("geom_point")
p5 <- ggplot(df) +
geom_jitter(aes(x, y, color = var), width = 0.3, height = 0.3) +
coord + ggtitle("geom_jitter")
p6 <- ggplot(df) +
ggpointgrid::geom_pointrect(aes(x, y, color = var)) +
coord + ggtitle("geom_pointrect")
p7 <- ggplot(df) +
ggpointgrid::geom_pointrect(
aes(x, y, color = var),
scale_x = 0.2,
scale_y = 0.2
) +
coord + ggtitle("geom_pointrect, scaling set")
cowplot::plot_grid(p4, p5, p6, p7)
```