5

I'm creating a polar plot showing a histogram of the direction travlled of paired group data. More specifically, directions travelled between different groups of sibling.

Here is a mock:

mockdf <- data.frame(dir = as.numeric( runif( 1000, -pi/2, pi) ),
                 ID = sample(letters[1:2], 1000, TRUE))
ggplot(data=mockdf, aes(x=mockdf$dir)) +
  coord_polar(theta = "x", start = pi, direction = 1) +
  scale_fill_manual(name = "Sibling", values=c("black", "White")) +
  geom_histogram(bins=32, aes(fill=mockdf$ID), color= "black") +
  facet_wrap(~mockdf$ID) +
  scale_y_continuous("Number of reloactions", limits = c(-8,30)) +
  scale_x_continuous(limits = c(-pi,pi), breaks = c(0, pi/4, pi/2, 3*pi/4, 
  pi, -3*pi/4, -pi/2, -pi/4),
                     labels = c("N", "NE", "E", "SE", "S", "SW", "W", "NW"))

Polar Plot Example

I'd like to move the y axis labels of 0, 10, 20, 30 onto the grid itself (i.e. along the SW orientation), but having difficulties doing so. Does anyone know how I go about doing this?

M--
  • 25,431
  • 8
  • 61
  • 93

1 Answers1

0

You can use options within theme to remove current axis text and tick marks andgeom_text() to manually add the labels. I've demonstrated how to add the first one.

ggplot(data=mockdf, aes(x=mockdf$dir)) +
  coord_polar(theta = "x", start = pi, direction = 1) +
  scale_fill_manual(name = "Sibling", values=c("black", "White")) +
  geom_histogram(bins=32, aes(fill=mockdf$ID), color= "black") +
  facet_wrap(~mockdf$ID) +
  scale_y_continuous("Number of reloactions", limits = c(-8,30)) +
  scale_x_continuous(limits = c(-pi,pi), breaks = c(0, pi/4, pi/2, 3*pi/4, 
                                                pi, -3*pi/4, -pi/2, -pi/4),
                 labels = c("N", "NE", "E", "SE", "S", "SW", "W", "NW")) + 
  # remove text and tick marks from axis
   theme(axis.text.y = element_blank(),
         axis.ticks.y = element_blank()) + 
  # add label manually
  geom_text(x = 4, y = 10, label = "10") 

plot example with text annotation

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
ShanEllis
  • 39
  • 3