2

I'm working with a dataset of 5k finish times that looks a little bit like this:

"15:34"
"14:23"
"17:34"

and so on, there's a lot, but they're all formatted like that. I'm able to convert all of them to POSIXct, and store them in a data frame to make using ggplot2 easier, but for the life of me, I cannot get ggplot to change colors. The fill command doesn't work, the graph just remains grey.

I've tried just referencing the POSIXct object I made, but ggplot throws an error and tells me it doesn't work well with POSIXct. The only way I've been able to display a histogram is by storing it in a dataframe.

The code I'm currently using looks like:

#make the data frame
df <- data.frame(
finish_times = times_list)


#set the limits on the x axis as datetime objects
 lim <- as.POSIXct(strptime(c('2018-3-18 14:15', '2018-3-18 20:00'), format = "%Y-%m-%d %M:%S"))

#making the plot
ggplot(data = df, aes(x = finish_times)) + 
  geom_histogram(fill = 'red') + #this just doesn't work
  stat_bin(bins = 30) + 
  scale_x_datetime(labels = date_format("%M:%S"),
                   breaks = date_breaks("1 min"),
                   limits = lim) +
  labs(title = "2017 5k finishers", 
       x='Finish Times',
       y= 'Counts')

I've crawled through a lot of ggplot and R documentation, and I'm not sure what I'm missing, I appreciate all help, thanks

  • Are you sure the ggplot code isn't throwing an error and just leaving up your last plot? `fill = "red"'` should work, e.g. try: `ggplot() + geom_histogram(aes(x=rnorm(100)), fill = 'red')` – Scott Ritchie Mar 19 '18 at 02:13
  • plotting packages have so many different pieces to them, it can be difficult to put things together from the documentation alone. A good place to start if you're new to ggplot would be http://r4ds.had.co.nz – De Novo Mar 19 '18 at 02:17

1 Answers1

5

stat_bin(bins = 30) is overriding anything you set in geom_histogram(). Generally, each geom has an associated default stat, and you can plot the object using one of the two, but when you try to do it with both, you can end up with problems. There are several solutions to this. Here's an example.

ggplot(diamonds, aes(x = carat)) + geom_histogram(fill = "red") + stat_bin(bins = 30)

Produces a plot with gray fill

enter image description here

ggplot(diamonds, aes(x = carat)) + geom_histogram(fill = "red", bins = 30)

Produces a plot with red fill

enter image description here

De Novo
  • 7,120
  • 1
  • 23
  • 39
  • Oh my god thank you so much, I've been trying to figure this out for ever! – Matthew Rogers Mar 19 '18 at 02:31
  • You're quite welcome :) Feel free to upvote as well as accept this as an answer. I think you can do that now. – De Novo Mar 19 '18 at 02:32
  • `stat_bin()` does not override the color when I try: `ggplot() + geom_histogram(aes(x=rnorm(100)), fill = "red") + stat_bin(bins = 30) ` – Scott Ritchie Mar 19 '18 at 03:40
  • I'm guessing that's because you set a mapping value, with aes so that impacts how successive calls inherit, but you'd probably have to step through it with a browser to be sure – De Novo Mar 19 '18 at 03:50