-
Notifications
You must be signed in to change notification settings - Fork 0
/
quollr3hexbin.Rmd
93 lines (77 loc) · 2.83 KB
/
quollr3hexbin.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
---
title: "3. Algorithm for binning data"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{3. Algorithm for binning data}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
options(rmarkdown.html_vignette.check_title = FALSE)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
warning = FALSE,
message = FALSE
)
```
Here, we'll discuss the algorithm for binning data.
```{r setup}
library(quollr)
library(ggplot2)
library(dplyr)
```
By passing the preprocessed 2D embedding data and hexagonal grid configurations, you can obtain the hexagonal binning information like centroid coordinates, hexagonal polygon coordinates, the standardise counts within each hexagon etc.
```{r}
r2 <- diff(range(s_curve_noise_umap$UMAP2))/diff(range(s_curve_noise_umap$UMAP1))
hb_obj <- hex_binning(data = s_curve_noise_umap_scaled, bin1 = 6, r2 = r2)
```
```{r}
## Data set with all possible centroids in the hexagonal grid
all_centroids_df <- hb_obj$centroids
glimpse(all_centroids_df)
## Generate all coordinates of hexagons
hex_grid <- hb_obj$hex_poly
glimpse(hex_grid)
## To obtain the standardise counts within hexbins
counts_df <- hb_obj$std_cts
df_bin_centroids <- extract_hexbin_centroids(centroids_df = all_centroids_df,
counts_df = counts_df) |>
filter(drop_empty == FALSE)
```
```{r}
ggplot(data = hex_grid, aes(x = x, y = y)) +
geom_polygon(fill = "white", color = "black", aes(group = hex_poly_id)) +
geom_point(data = all_centroids_df, aes(x = c_x, y = c_y), color = "red") +
coord_fixed()
```
```{r}
ggplot(data = hex_grid, aes(x = x, y = y)) +
geom_polygon(fill = "white", color = "black", aes(group = hex_poly_id)) +
geom_point(data = all_centroids_df, aes(x = c_x, y = c_y), color = "red") +
geom_point(data = df_bin_centroids, aes(x = c_x, y = c_y), color = "purple") +
coord_fixed()
```
```{r}
ggplot(data = hex_grid, aes(x = x, y = y)) +
geom_polygon(fill = "white", color = "black", aes(group = hex_poly_id)) +
geom_point(data = s_curve_noise_umap_scaled, aes(x = UMAP1, y = UMAP2), color = "blue") +
coord_fixed()
```
```{r}
hex_grid_with_counts <- left_join(hex_grid, counts_df, by = c("hex_poly_id" = "hb_id"))
ggplot(data = hex_grid_with_counts, aes(x = x, y = y)) +
geom_polygon(color = "black", aes(group = hex_poly_id, fill = std_counts)) +
geom_text(data = all_centroids_df, aes(x = c_x, y = c_y, label = hexID)) +
scale_fill_viridis_c(direction = -1, na.value = "#ffffff") +
coord_fixed()
```
You can also use `geom_hexgrid` to visualise the hexagonal grid rather than `geom_polygon`.
```{r}
ggplot(data = all_centroids_df, aes(x = c_x, y = c_y)) +
geom_hexgrid() +
coord_equal() +
xlab("x") + ylab("y") +
theme(axis.text = element_text(size = 5),
axis.title = element_text(size = 7))
```