Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if y-axis begins at zero

Tags:

r

ggplot2

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.

like image 263
Paul Avatar asked Jun 25 '19 17:06

Paul


People also ask

Does my Y axis have to start at 0?

Data in a line chart is encoded by position (x, y coordinates), whereas in a bar chart data is represented by length. This subtle difference changes the way a reader uses the chart, meaning that in a line chart it's ok to start the axis at a value other than zero, despite many claims that they are always misleading.

Do graphs start at 0?

While it's a good idea to have best practices with displaying data in graphs, the “show the zero” is a rule that clearly can be broken. But showing or not showing the zero alone is not sufficient to declare a graph objective or conversely “deceptive.”

Why should the Y axis start from 0 in a bar chart?

In the case of bar charts, this means that the y-axis must always start at zero. The bars in a bar chart encode the data by their length, so if we truncate the length by starting the axis at something other than zero, we distort the visual in a bad way.

Should the Y axis of a graph start at zero?

The Y axis for my graphs do not start at zero and my teacher has been very specific that this has to be handled carefully so as not to misrepresent the data. I know that I can indicate this with the two little lines (don't know what the technical term is) on the Y axis where zero would have been.

Should the Y-axis always start at zero?

However, "always start the y-axis at zero" is not a hard-and-fast rule. For example, Edward Tufte points out that in a time series, the baseline is not necessarily zero: In general, in a time-series, use a baseline that shows the data not the zero point.

Should a line chart start at zero?

(Bar charts should always start at zero – I’ll explain why in my next post.) It’s ok to start a line chart at a non-zero value but there are some things you should be aware of before you do so. As soon as you truncate your axis you’re making an editorial decision to focus on the data in a different way, so take care not to mislead the reader.

Should scatter plot axes start at zero?

Scatter plots use the same positional method of encoding each data point, but I have never heard anyone say that scatterplot axes should start at zero. In most cases, a zero-based axis makes sense, but it ultimately depends on the data and visualization used.


1 Answers

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)
like image 96
thc Avatar answered Nov 15 '22 08:11

thc