Experiment
library(tidyverse)
compute_panel_textcircle <- function(
data, scales, r = 3, x0 = 0, y0 = 0,
start = 45, end = -45) {
dplyr::mutate(
data,
theta = seq(((pi * start) / 180), pi * (2 + ((end) / 180)),
length.out = nrow(data)
),
x = x0 + r * cos(.data$theta),
y = y0 + r * sin(.data$theta),
angle = 180 + 360 * (.data$theta / (2 * pi))
)
}
StatTextcircle <- ggplot2::ggproto(
`_class` = "StatTextcircle",
`_inherit` = ggplot2::Stat,
required_aes = "label",
compute_panel = compute_panel_textcircle
)
state.name %>%
data.frame(state = .) %>%
ggplot() +
aes(label = state) +
geom_label(stat = StatTextcircle, hjust = 1)
last_plot() +
aes(x = 5) # 'hard coding' in compute_panel means you can't make an adjustment
last_plot() +
aes(color = str_detect(state, "[AEIOU]"))
StatTextcircle$default_aes <- ggplot2::aes(
x = ggplot2::after_stat(x),
y = ggplot2::after_stat(y)
)
state.name %>%
data.frame(state = .) %>%
ggplot() +
aes(label = state) +
geom_label(stat = StatTextcircle, hjust = 1) +
aes(x = 5)
compute_panel_textcircle2 <- function(
data,
scales,
r = 3,
x0 = 0,
y0 = 0,
start = 45,
end = -45) {
dplyr::mutate(
data,
theta = seq(((pi * start) / 180), pi * (2 + ((end) / 180)),
length.out = nrow(data)
),
xpos = x0 + r * cos(.data$theta),
ypos = y0 + r * sin(.data$theta),
angle = 180 + 360 * (.data$theta / (2 * pi))
)
}
StatTextcircle2 <- ggplot2::ggproto(
`_class` = "StatTextcircle2",
`_inherit` = ggplot2::Stat,
required_aes = "label",
compute_panel = compute_panel_textcircle2,
default_aes = ggplot2::aes(
x = ggplot2::after_stat(xpos),
y = ggplot2::after_stat(ypos)
)
)
state.name %>%
data.frame(state = .) %>%
ggplot() +
aes(label = state) +
geom_text(stat = StatTextcircle2, hjust = 1)
last_plot() +
aes(x = 0)
last_plot() +
aes(x = after_stat(xpos)) +
aes(y = 0)