1

I am trying to replace the x axis of a histogram with its month, the data looks similar to:

library(tidyverse)
library(lubridate)
library(okcupiddata) # the example data

df <- profiles %>% as_tibble() %>% 
  select(last_online) %>% 
  mutate(month = month(last_online, label = TRUE, abbr = FALSE),
         day   = yday(last_online)) 

# A tibble: 59,946 x 3
   last_online         month   day
   <dttm>              <dbl> <dbl>
 1 2012-06-28 20:30:00  June   180
 2 2012-06-29 21:41:00  June   181
 3 2012-06-27 09:10:00  June   179
 4 2012-06-28 14:22:00  June   180
 5 2012-06-27 21:26:00  June   179

now I want to create a histogram with the days of the year

df %>%
   ggplot(aes(x = day, fill = ..count..)) +
   geom_histogram(bins = 365) + 
   scale_y_log10()

enter image description here

I want to replace the day-axis with it assigned month variable. I tried to use scale_x_discrete(labels = month), but this is just deleting the axis.

I assume I need to perform a larger transformation or programming, but I hope there is already a function that can quickly be applied.

I ultimately want to create a radial plot (adding + coord_polar()) with the month as a break, similar to this:

enter image description here

Stephan
  • 2,056
  • 1
  • 9
  • 20
  • 1
    If it is only about the x-axis scales, you might find a hint here: https://stackoverflow.com/questions/10576095/formatting-dates-with-scale-x-date-in-ggplot2 (scale_x_date(date_breaks = "1 month", ...) – Wolfgang Arnold Sep 25 '18 at 09:13
  • thank you, my problem is, that my `x` is a numeric input, not a date. – Stephan Sep 25 '18 at 09:35
  • Sorry, forget my comment - here's an option: ggplot(df, aes(x = day, fill = ..count..)) + geom_histogram(bins = 365) + scale_y_log10() + scale_x_continuous(breaks = seq(30, 365, by = 30), labels = month.name) – Wolfgang Arnold Sep 25 '18 at 10:40
  • Or: ...labels = unique(df$month).. – Wolfgang Arnold Sep 25 '18 at 10:46

0 Answers0