可能有点想得太多了,但这似乎是硬编码的
render_fg
的
CoordPolar
library(ggplot2)
library(grid)
# Constructor
alt_coord_polar <- function(...) {
polar_coord <- coord_polar(...)
# Very ad-hoc changes to the previous class
coord <- ggproto(
NULL, polar_coord,
render_fg = function(self, panel_params, theme) {
if (is.null(panel_params$theta.major)) {
return(element_render(theme, "panel.border"))
}
theta <- ggplot2:::theta_rescale(self, panel_params$theta.major,
panel_params)
labels <- panel_params$theta.labels
theta <- theta[!is.na(theta)]
ends_apart <- (theta[length(theta)] - theta[1]) %% (2*pi)
if (length(theta) > 0 && ends_apart < 0.05 && !is.null(labels)) {
n <- length(labels)
if (is.expression(labels)) {
combined <- substitute(paste(a, " ", b), # <- changed middle
list(a = labels[[1]], b = labels[[n]]))
} else {
combined <- paste(labels[1], labels[n], sep = " ") # <- changed sep
}
labels[[n]] <- combined
labels <- labels[-1]
theta <- theta[-1]
}
grobTree(
if (length(labels) > 0) element_render(
theme, "axis.text.x",
labels,
unit(0.45 * sin(theta) + 0.5, "native"),
unit(0.45 * cos(theta) + 0.5, "native"),
hjust = 0.5, vjust = 0.5
),
element_render(theme, "panel.border")
)
}
)
}
然后使用您自己的自定义极坐标:
lungdeath <-
structure(list(deaths = c( 2134, 1863, 1877, 1877, 1492, 1249, 1280, 1131, 1209, 1492, 1621, 1846, 1846
), month = c( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0
), year = c( 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974
)), row.names = c( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "121"
), class = "data.frame")
ggplot(lungdeath) +
geom_line(aes(month, deaths)) +
alt_coord_polar() +
scale_x_continuous(breaks = 0:12, labels = c("undesired",1:12))
于2021-04-21由
reprex package