Saswat Mishra
Saswat Mishra

Reputation: 195

ggplot polar_coord space out text better

I have some data in the data frame times.posix. Since it's time data, I want to visualize it as a circular histogram. My code is this:

ggplot(times.posix) +
  geom_histogram(aes(x = pct.hour(dropoff.posix) ), bins = 24, color = 'black', fill = 'blue') +
  scale_x_continuous(breaks = 0:23, labels = paste( (11:(11+11) )%%12 + 1, c(rep('AM',12), rep('PM', 12) ) ) ) +
  ggtitle('Dropoff time frequency') +
  coord_polar()

and my graph ends up like this

image

The actual geom_hist ends up fine but the text around it is weirdly spaced - the labels seem alright starting at 12AM but by 11PM theyre a full block behind. Anyone know how to fix this?

Upvotes: 0

Views: 60

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173898

Your problem seems to be with the pct.hour function. I'm not sure what this function is, since the required libraries aren't included in the question and I can't find any documentation online.

If you instead use lubridate::hour, the problem seems to vanish:

ggplot(times.posix) +
  geom_histogram(aes(x = lubridate::hour(dropoff.posix)), 
                 bins = 24, 
                 color = 'black', 
                 fill = 'blue') +
  scale_x_continuous(breaks = 0:23, 
                     labels = paste((11:(22)) %% 12 + 1, c(rep('AM', 12), rep('PM', 12)))) +
  ggtitle('Dropoff time frequency') +
  coord_polar()

enter image description here

However, I can replicate your undesired plot if any of the values of x are 24, for example:

geom_histogram(aes(x = lubridate::hour(dropoff.posix) * 24/23))

Gives

enter image description here

So my guess is that if you do max(pct.hour(times.posix$dropoff.posix)) you will get 24, when it should be 23. If you let us know what the pct.hour() function is, perhaps we can help see where the problem really lies.


Data used

set.seed(69)
times.posix <- data.frame(dropoff.posix = as.POSIXct("2020-03-13") + 60 * runif(500,0,1440))

Upvotes: 2

Related Questions