library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.0 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.1 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
create_circle <- function(data, n){
angles <- seq(from = 0,
to = 2 * pi,
length.out = n + 1)
data.frame(
x = cos(angles) * data$r + data$x0,
y = sin(angles) * data$r + data$y0,
data
)
}
StatCircle <- ggproto(`_class` = "StatCircle",
`_inherit` = Stat,
setup_data = function(data, params) {
if (data$group[1] == -1) {
nrows <- nrow(data)
data$group <- seq_len(nrows)
}
data # return data with a group variable
},
compute_group = function(data, scales, n = 5){create_circle(data, n = n)},
required_aes = c("x0", "y0", "r")
)
circles <- data.frame(x0 = c(-5,5), y0 = c(5, -5),
r = c(5, 4), class = c("A", "B"))
ggplot(circles) +
geom_polygon(stat = StatCircle,
aes(x0 = x0, y0 = y0,
r = r, fill = class))
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
ggplot(circles) +
geom_polygon(stat = StatCircle,
aes(x0 = x0, y0 = y0,
r = r, fill = class)) +
facet_wrap(~ class)
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
penguins |>
slice(1:7, .by = species) |>
mutate(id_penguin = row_number()) |>
ggplot() +
aes(x0 = bill_len, y0 = bill_dep, r = .2) +
geom_polygon(stat = StatCircle)
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_circle()`).
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
last_plot() +
aes(fill = species)
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_circle()`).
## Warning in cos(angles) * data$r: longer object length is not a multiple of
## shorter object length
## Warning in sin(angles) * data$r: longer object length is not a multiple of
## shorter object length
## Warning in cos(angles) * data$r: longer object length is not a multiple of
## shorter object length
## Warning in sin(angles) * data$r: longer object length is not a multiple of
## shorter object length
last_plot() +
aes(group = id_penguin)
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_circle()`).
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
What worked for me was to remove the conditional
data$group[1] == -1fromStatCircleand just replacing any existinggroupvalue with row indices.
This might be inadvisable, since in general it’s bad to overwrite the user’s
groupassignment; but i think it would be a rare situation for something that behaves likeStatIdentitywithGeomPointto fail because of a badgroupassignment. My understanding is thatgrouponly really matters to the statistical transformation, e.g. grouping points before computing an ellipse or convex hull; so here it’s safe to “appropriate” it for another purpose since there’ll be no conflict.
An alternative would (i think) be for the stat to define a new, custom
group-like column (a computed variable) that would be recognized by a companion geom. This violates the “pairability principle” one might call it—that new stats and geoms should be designed to pair with existing geoms and stats—but that’s a principle i personally like that i think most extenders don’t follow closely. (We’ve talked about it in discussions.) But it might be more appropriate because it acknowledges that the structure of the graphical element—differently-sized circles around points that may also be grouped by color and opacity—is different from the simpler structure of a collection of paths that would be passed toGeomPolygon.
StatCircle <- ggproto(`_class` = "StatCircle",
`_inherit` = Stat,
setup_data = function(data, params) {
# if (data$group[1] == -1) {
nrows <- nrow(data)
data$group <- seq_len(nrows)
# }
data # return data with a group variable
},
compute_group = function(data, scales, n = 5){create_circle(data, n = n)},
required_aes = c("x0", "y0", "r")
)
penguins |>
slice(1:7, .by = species) |>
mutate(id_penguin = row_number()) |>
ggplot() +
aes(x0 = bill_len, y0 = bill_dep, r = .2) +
geom_polygon(stat = StatCircle) +
aes(color = species)
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_circle()`).
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
knitr::knit_exit()