1

I have a bar plot in ggplot. When I set limits for the y axis I get an empty plot, as if the frame I set was outside of the data. I have tried both scale_y_continuous and the shorthand ylim.

The initial plot, which creates a meaningful graph:

p <- ggplot(data=subset(df), aes(x=Date, y=Time, fill=Total)) +
geom_bar(stat="identity") +
scale_y_continuous(breaks=c(8,10,12,14,16,18),
    labels=c("8 am", "10 am", "12 pm", "2 pm", "4 pm", "6 pm"))`

The structure of df:

'data.frame':   25 obs. of  3 variables:
$ Date : Date, format: "2015-07-20" "2015-07-21" "2015-07-22" ...
$ Time : num  11.24 10.3 10.51 9.64 12.9 ...
$ Total: int  7 9 9 4 6 1 6 7 6 10 ...

Plot with limits, which produces an empty frame:

p <- ggplot(data=subset(df), aes(x=Date, y=Time, fill=Total)) + 
geom_bar(stat="identity") +
scale_y_continuous(breaks=c(8,10,12,14,16,18), 
    labels=c("8 am", "10 am", "12 pm", "2 pm", "4 pm", "6 pm"),
    limits=(c(7,18)`

Any ideas why adding limits loses the data? I don't ever receive an error.

Unrelated
  • 347
  • 2
  • 14
  • Your syntax is incorrect. `limits` isn't a function that you want to call (it is an unexported ggplot function, but you shouldn't need to call it directly). Instead just use the limits *argument* setting it equal to your range vector `labels = ..., limits = c(7, 18)`. – Gregor Thomas Aug 27 '15 at 23:09
  • Thanks @Gregor. That was just a transposing error--not that way in my actual code. – Unrelated Aug 28 '15 at 00:42
  • I'm trying to find the best duplicate to close this of...in the meantime read up on `coord_cartesian`. – joran Aug 28 '15 at 00:45

0 Answers0