Home > front end >  GGplot circular: Data dissapears when trying to format x-axis
GGplot circular: Data dissapears when trying to format x-axis

Time:02-08

I am trying to create a circular plot showing the number of movements fish have during each hour. It works fine if the code is like this:

ggplot(aragx, aes(x = eventhour, y = Changes, group=eventhour, col=Family))   
  geom_boxplot(position=position_dodge())  
  scale_x_continuous(breaks = seq(0, 23), labels = seq(0, 23))   
  coord_polar(start = 0)   theme_minimal()   
  scale_fill_brewer()   ylab("Count")   
  ggtitle("Daily Section Changes per Hour")  ylim(0,20)  facet_wrap(.~Family)  
  theme(legend.position = "none")   theme(axis.title.x = element_blank())

However, the 0-12 line doesn't quite run straight to the middle, the angle between 23 and 0 isn't straight and it just doen't look nice. So I modify scale_x_continuous as follows:

ggplot(aragx, aes(x = eventhour, y = Changes, group=eventhour, col=Family))   
  geom_boxplot(position=position_dodge())  
  scale_x_continuous(limits = c(0,24), breaks = seq(0, 23), labels = seq(0, 23))   
  coord_polar(start = 0)   theme_minimal()   
  scale_fill_brewer()   ylab("Count")   
  ggtitle("Daily Section Changes per Hour")  ylim(0,20)  facet_wrap(.~Family)  
  theme(legend.position = "none")   theme(axis.title.x = element_blank())

This fixes the cosmetic issue, but the data from eventhour=0 is all screwed up, like so:

enter image description here

Does anyone know how to help me? It would be much appreciated, I've been banging my head against the wall over this small thing.

CodePudding user response:

The issue is that by setting limits = c(0, 24) the parts of your box plot which range to the left of 0 (or to the right of 24) are clipped off. Hence, for the boxplot at the zero position only the whiskers and the right segment of the box are drawn.

To prevent that you have to adjust the limits to take account of the width of the boxplot which by default is .75. Hence you could get a full boxplot at the zero position set limits = c(-width_bp / 2, 24 - width_bp / 2). However, doing so will rotate your circular plot slightly which we could compensate for by setting start in coord_polar eqaul to -width_bp / 8 (Note: I checked that out by trial and error but there is for sure a reason why it has to be one eigth. Sigh, was always better in algebra than in geometry. (; ).

Using some random fake example data:

library(ggplot2)

aragx <- data.frame(
  eventhour = rep(0:23, 100),
  Changes = runif(24 * 100)
)
width_bp <- .75
ggplot(aragx, aes(x = eventhour, y = Changes, group = eventhour))  
  geom_boxplot(position = position_dodge())  
  scale_x_continuous(
    limits = c(-width_bp / 2, 24 - width_bp / 2),
    breaks = seq(0, 23), labels = seq(0, 23)
  )  
  coord_polar(start = -width_bp / 8)  
  theme_minimal()  
  scale_fill_brewer()  
  ylab("Count")  
  ggtitle("Daily Section Changes per Hour")  
  theme(legend.position = "none")  
  theme(axis.title.x = element_blank())

Created on 2022-02-03 by the reprex package (v2.0.1)

  •  Tags:  
  • Related