View source: R/position-sunflower.R
sunflower | R Documentation |
This function distributes points in a ellipse via a sunflower seed algorithm as a solution for over-plotting. To implement the algorithm, this function adapts the code from https://stackoverflow.com/questions/28567166/uniformly-distribute-x-points-inside-a-circle.
sunflower(x = NULL, y = NULL, density, aspect_ratio)
x , y |
The identical coordinates of multiple over-plotted points, as vectors, which will be arranged using a sunflower seed algorithm. |
density |
The pattern density. |
aspect_ratio |
An aspect ratio adjustment to compensate for distortion of the circular arrangement, which might occur when plotting if coord_equal() is not used. A wide aspect ratio (eg. 2) would adjust for vertical stretching, whereas a tall aspect ratio (eg. 0.5) would adjust for horizontal stretching. An aspect ratio of 1 is appropriate when no adjustment is required. |
A numeric vector of adjusted x
or y
positions, computed using a sunflower seed algorithm.
Other Functions:
position_circlepack()
,
position_circlepackdodge()
,
position_jitter_ellipse()
,
position_jitterdodge_ellipse()
,
position_sunflower()
,
position_sunflowerdodge()
library(ggplot2)
library(dplyr)
# Manually adjust position of N points,
# arranging them per the sunflower algorithm and then dodging groups
N <- 300
dat <- data.frame(
x = sample(1:2, size = N, replace = TRUE),
y = sample(1:7, size = N, replace = TRUE),
type = factor(sample(LETTERS[1:2], N, replace = TRUE))
) |>
group_by(x, y, type) |>
mutate(
x = sunflower(x = x, density = 1, aspect_ratio = 1),
y = sunflower(y = y, density = 1, aspect_ratio = 1),
x = if_else(type == "A", x - (1 / 8), x + (1 / 8))
)
ggplot(dat, aes(x, y, color = type, shape = type)) +
geom_point() + coord_equal()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.