You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Setting breaks = NULL, to remove axis tick marks and labels, results in the axis being dropped altogether. I don't remember seeing this before. Is this an intended feature?
No, it‘s two separate issues. The other issue is simply caused by the discrete scale only placing breaks at discrete values found in the data, which is correct behavior I think.
Thanks for the feedback Claus. So I think I've got something. Please let me know what you think.
The first argument of the guide_axis function in "guides-axis.r" returns zeroGrob() if at has length zero, which it is the case if we set breaks = NULL.
The code copied below makes it clear why breaks = NULL and breaks = numeric(0) have the same effect:
guide_axis <- function(at, labels, position = "right", theme) {
if (length(at) == 0) {
return(zeroGrob())
}
Now, two things. How to fix this issue such that breaks = NULL results in breaks and labels being removed, but the axis being kept? Secondly, when was this break introduced and was it intended?
Hadley changed NA to NULL (among other changes). I briefly experimented with interpreting NULL breaks as NA and a simple example worked as expected. I have not, however, tested further, as it's possible that Hadley's change was intended. In "guides-axis.r", change lines 7-10 to the following:
if (length(at) == 0) {
at <- NA; print("I'm line 9 in guides-axis.r");
#return(zeroGrob())
}
Above I bypass returning zeroGrob() entirely. It fits my expectation that removing breaks should not remove the axis line. To keep the ability to remve the axis line, without going through a theme, we could add a variable like axis that could be set to null with axis = NULL inside the scale_*_* funtions. Or show.axis = FALSE or something like that.
As a side comment, while exploring the code I noticed that a break_info function is defined multiple times. Isn't that supposed to be a bad idea? Below are the places where it occurs, in slightly different guises, but with pretty much the same purpose:
scale-.r, line 317
scale-continuous.r, line 126
scale-date.r, line 335
axis-secondary.R, line 151
While I use ggplot2 nearly every day, I've never looked very long inside the hood and there's much of the structure that is a complete mystery to me. I had a few minutes to spare...
It seems to me that setting "breaks = NULL" ought to remove the axis ticks and labels, but NOT the axis itself. It seems to me that the appearance of the axis lines (including their possible invisibility) ought to be controlled within the theme functions.
As I recall it and as illustrated by various stackoverflow discussions, the old behaviour was to remove tick marks and labels, but not the line. See e.g. this question from 2010 (ancient history): https://stackoverflow.com/questions/2678141/. Remark: There is one example from the current PDF manual that gives a clear indication that setting "breaks = NULL" is intended to remove the axis. It is in the section on the (otherwise pretty much broken) geom_dotplot() (currently, page 67). Elsewhere in the manual, it is not explained so clearly what "breaks = NULL" is intended to achieve.
Moreover, it seems to me that the usage of "breaks = NULL" ought to be more consistent across different geoms/scales/stats. Currently the behaviour of "breaks = NULL" is to remove the axis when it is set inside a scale_*_*()'' function, but it doesn't do anything when it is set inside the stat_bin() function (here the breaks optional argument is intended to allow the user to override the default computations, with default set to "breaks = NULL").
Suggestion 1: replace the default "breaks = NULL" inside stat_bin() to "breaks = NA" (simple change of the check from is.null to is.na).
Suggestion 2: change the default behaviour of "breaks = NULL" from suppressing the entire axis to suppressing only the ticks and labels.
Any opinion?
library(ggplot2)
theme_set(theme_classic()).# to show the axes, if any# Set breaks = NULL inside scale_y_continuous, current behaviour: one example in the PDF documentation:# From manual page 67, Examples of geom_dotplot # y axis isn't really meaningful, so hide it
ggplot(mtcars, aes(x=mpg)) + geom_dotplot(binwidth=1.5) +
scale_y_continuous(NULL, breaks=NULL)
# In stat_bin(), the default is "breaks = NULL" # Default
ggplot(mtcars, aes(x=mpg)) + stat_bin()
# Set breaks = NULL: does not suppress axis, neither does it suppress labels
ggplot(mtcars, aes(x=mpg)) + stat_bin(breaks=NULL)
# Set breaks = NA: seems like NA ought to be a workable alternative to NULL
ggplot(mtcars, aes(x=mpg)) + stat_bin(breaks=NA)
#> Error: <text>:3:27: unexpected symbol#> 2: library(ggplot2)#> 3: theme_set(theme_classic()).#> ^
Activity
ptoche commentedon Nov 7, 2018
I have not looked at the code (note to self: look at the code), but could this be related to this other case of the missing axis?
#2970
clauswilke commentedon Nov 7, 2018
No, it‘s two separate issues. The other issue is simply caused by the discrete scale only placing breaks at discrete values found in the data, which is correct behavior I think.
ptoche commentedon Nov 10, 2018
Thanks for the feedback Claus. So I think I've got something. Please let me know what you think.
The first argument of the
guide_axisfunction in "guides-axis.r" returnszeroGrob()ifathas length zero, which it is the case if we setbreaks = NULL.The code copied below makes it clear why
breaks = NULLandbreaks = numeric(0)have the same effect:Now, two things. How to fix this issue such that
breaks = NULLresults in breaks and labels being removed, but the axis being kept? Secondly, when was this break introduced and was it intended?Line 7 of 2363611#diff-348ffaa265a58dc2db9470750b00523bL7
Hadley changed
NAtoNULL(among other changes). I briefly experimented with interpretingNULLbreaks asNAand a simple example worked as expected. I have not, however, tested further, as it's possible that Hadley's change was intended. In "guides-axis.r", change lines 7-10 to the following:Above I bypass returning
zeroGrob()entirely. It fits my expectation that removing breaks should not remove the axis line. To keep the ability to remve the axis line, without going through a theme, we could add a variable likeaxisthat could be set to null withaxis = NULLinside thescale_*_*funtions. Orshow.axis = FALSEor something like that.As a side comment, while exploring the code I noticed that a
break_infofunction is defined multiple times. Isn't that supposed to be a bad idea? Below are the places where it occurs, in slightly different guises, but with pretty much the same purpose:While I use
ggplot2nearly every day, I've never looked very long inside the hood and there's much of the structure that is a complete mystery to me. I had a few minutes to spare...Here the only example I have tested:
ptoche commentedon Nov 21, 2018
What do we mean when we set breaks to NULL?
It seems to me that setting
"breaks = NULL"ought to remove the axis ticks and labels, but NOT the axis itself. It seems to me that the appearance of the axis lines (including their possible invisibility) ought to be controlled within the theme functions.As I recall it and as illustrated by various stackoverflow discussions, the old behaviour was to remove tick marks and labels, but not the line. See e.g. this question from 2010 (ancient history): https://stackoverflow.com/questions/2678141/. Remark: There is one example from the current PDF manual that gives a clear indication that setting
"breaks = NULL"is intended to remove the axis. It is in the section on the (otherwise pretty much broken)geom_dotplot()(currently, page 67). Elsewhere in the manual, it is not explained so clearly what"breaks = NULL"is intended to achieve.Moreover, it seems to me that the usage of
"breaks = NULL"ought to be more consistent across different geoms/scales/stats. Currently the behaviour of"breaks = NULL"is to remove the axis when it is set inside ascale_*_*()''function, but it doesn't do anything when it is set inside thestat_bin()function (here thebreaksoptional argument is intended to allow the user to override the default computations, with default set to"breaks = NULL").Suggestion 1: replace the default
"breaks = NULL"insidestat_bin()to"breaks = NA"(simple change of the check fromis.nulltois.na).Suggestion 2: change the default behaviour of
"breaks = NULL"from suppressing the entire axis to suppressing only the ticks and labels.Any opinion?
Created on 2018-11-21 by the reprex package (v0.2.0).
lock commentedon Oct 20, 2019
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/