How to change y axis from count to percentage in rose diagram in ggplot?

188 views Asked by At

I created a rose diagram based on the example here, but I need the y axis to show each bin as a percentage of the total. The data I am actually using is a vector of the different orientations of a group of animals. My example code currently would be something like:

Degrees <- runif(100, 0, 360)
# 45 degree bins
rose <- ggplot(mapping = aes(x = Degrees)) +
  stat_bin(breaks = (0:8 - 0.5)/8 * 360, color = "black", fill = "light grey") +
  scale_x_continuous(
    breaks = 0:7/8*360) +
  coord_polar(start=-pi/8)
rose

example rose plot generated

Thank you for any help you can give!

2

There are 2 answers

1
Onyambu On

You could simply add:

rose + scale_y_continuous(labels = scales::percent)

enter image description here

0
Liam Dickson On

I think I've figured this out: I've added a y aesthetic into stat_bin that seems to have fixed the issue.

# 45 degree bins
rose <- ggplot(mapping = aes(x = Degrees)) +
  stat_bin(aes(y = (..count..)/sum(..count..)), breaks = (0:8 - 0.5)/8 * 360, color = "black", fill = "light grey") +
  scale_x_continuous(
    breaks = 0:7/8*360) +
  coord_polar(start=-pi/8)
rose

Displays y as count/sum(count)