Reputation: 105
I want to determine if a plot generated by ggplot
begins at zero.
I am generating a couple hundred reports that each have thirty or more charts in them. I am content with ggplot
's defaults for when a plot starts at zero and when it doesn't, but I want to add a caption that draws the reader's attention to this fact.
Something like:
labs(caption = ifelse(XXXXX, "Note: y-axis does not begin at zero", ""))
But I have no clue what my test should be.
Upvotes: 6
Views: 46
Reputation: 9705
Try this:
library(ggplot2)
g <- ggplot(data.frame(x=1:10, y=0:9), aes(x=x,y=y)) + geom_point()
yrange <- ggplot_build(g)$layout$panel_params[[1]]$y.range
if(yrange[1] <= 0) g <- g + labs(caption = "Note: y-axis does not begin at zero")
plot(g)
Upvotes: 4