'Add guide lines for y-axis when using coord_polar() for polar histogram
I want to create a polar histogram like this
with dashed lines going from the y-axis ticks to the 0/360 line to help the reader to map the count to the histogram's bar length. I have added the dashed lines with power point but I am wondering whether it is possible to add this with ggplot.
I used this code
library(ggplot2)
# Seed
set.seed(20220315)
# Create random data from 0 to 360
example_dat <- data.frame(rot_y = runif(1000, min = 0, max = 360))
# Create polar plot
ggplot(example_dat, aes(x = rot_y )) +
geom_histogram(binwidth = 15, boundary = -7.5, colour = "black", size = .25) +
scale_x_continuous(limits = c(0,360),
breaks = seq(0, 360, by = 60),
minor_breaks = seq(0, 360, by = 15)) +
coord_polar() +
labs(title = 'Polar histogram of y rotation',
y = 'Count',
x = '')
to generate this plot
I am aware of the work around for drawing a line segment like in this solution: draw straight line between any two point when using coord_polar() in ggplot2 (R)
However, when drawing a straight line with function given in that solution using aes(x = 270, y = 0, xend = 720, yend = 60), the line just disappears, while a line can be seen for something like aes(x = 270, y = 0, xend = 720, yend = 10) but the problem is then it does not extent to the label ticks, which make sense as I told the line to end at 10.
Solution 1:[1]
Here's a slightly manual approach using patchwork, assuming the original plot is saved to p. The alignment of the plots uses magic numbers determined by trial and error.
library(patchwork)
l <- ggplot() +
annotate("segment", x = 0, xend = -60,
y = 10*c(0:5), yend = 10*c(0:5),
linetype = "dashed") +
theme_void()
p + inset_element(l, left = 0, top = 0.91, bottom = 0.48, right = 0.52)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jon Spring |



