Description
Hi folks,
I'm curious if there's a recommended way to annotate a wedge on radial axes which crosses 'North' (for want of a better term).
Say I have some radial data, like wind direction, and I want to annotate a wedge to draw the viewer's attention to a specific part. For most of the plot that's easy - pick a start and end (e.g., 90 and 180).
But if I want it to cross north it gets tricky - e.g., if I want it to go from due West (270 degr) to a North East direction (30 degr) my wedge either goes the wrong way around the plot, or needs splitting into two separate wedges which looks rubbish because you get two line borders.
Is it possible for a coord_radial()
plot to draw a single shape across this North line?
I appreciate the answer may be "no" as it'd be akin to looping around the edge of coord_cartesian()
, but I can't imagine this is an uncommon situation for polar coordinates where the start and end of your xlims are equivalent, even if ggplot2
internally doesn't think they are.
library(ggplot2)
wind <- data.frame(wd = seq(0, 340, 20), ws = sample(5:25, size = 18))
(
plt <-
ggplot(wind, aes(x = wd, y = ws)) +
geom_point() +
coord_radial(expand = F) +
expand_limits(y = 0) +
scale_x_continuous(
limits = c(0, 360),
breaks = c(0, 90, 180, 270),
labels = c("N", "E", "S", "W")
)
)
# okay if not crossing N
plt +
annotate(
xmin = 90,
xmax = 180,
ymin = -Inf,
ymax = Inf,
geom = "rect",
color = "black",
fill = NA
)
# not good if crossing N - I want the opposite of this
plt +
annotate(
xmin = 270,
xmax = 30,
ymin = -Inf,
ymax = Inf,
geom = "rect",
color = "black",
fill = NA
)
# can bodge, but you clearly get two shapes
plt +
annotate(
xmin = 270,
xmax = 360,
ymin = -Inf,
ymax = Inf,
geom = "rect",
color = "black",
fill = NA
) +
annotate(
xmin = 0,
xmax = 30,
ymin = -Inf,
ymax = Inf,
geom = "rect",
color = "black",
fill = NA
)
Created on 2025-03-01 with reprex v2.1.1