Reputation: 541
When I use ggplot, I generally format my code as such:
ggplot() +
geom_col() +
geom_line() +
scale_y_continuous()
This format neatly organizes the components of the plot, and RStudio lets you run all the lines by hitting Ctrl+Enter
. If I ever want to remove a component of the plot, I can simply comment that line out, and RStudio will ignore that line.
But if I comment out the last line,
ggplot() +
geom_col() +
geom_line() +
#scale_y_continuous()
I will get an error, because I did not remove the +
after geom_line()
. It is obviously easy to remove the +
sign, but then I have add it back when I un-comment the last line.
It's not a huge problem, but in the process of making a plot, I inevitably forget to add or remove the +
at least once. It is a bigger problem when working on a laptop with a small screen, where I cannot necessarily see the end of the last line.
Is there a function in ggplot which I can add below the last line, which will be read, but will do nothing, preventing me from having to deal with the +
signs?
For example, Python has the pass
statement, which acts as a filler when writing code. Does something similar exist in ggplot?
Upvotes: 3
Views: 350
Reputation: 2028
A perhaps safer option is to use NULL
. This does literally nothing whereas geom_blank()
adds a layer. The use of NULL
after a ggplot chain is a deliberate design feature.
library('ggplot2')
plot0 = ggplot(iris) +
geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Species))
plot1 = ggplot(iris) +
geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Species))+
# scale_y_continuous()+
NULL
plot2 =
ggplot(iris) +
geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Species))+
# scale_y_continuous()+
geom_blank()
length(plot0$layers)
#> [1] 1
length(plot1$layers)
#> [1] 1
length(plot2$layers)
#> [1] 2
object.size(plot0) < object.size(plot1)
#> [1] FALSE
object.size(plot0) < object.size(plot2)
#> [1] TRUE
Upvotes: 1
Reputation: 11
ggplot() + theme()
or
ggplot() + list()
or
ggplot() + invisible(list())
do nothing and do not add a layer. The last one also won't print anything if executed on its own.
Upvotes: 1
Reputation: 8676
You have two options
a. geom_blank()
b. gp <- gp +
.
The gp <- gp +
solution is my personal recommendation. It allows for simple addition and removal of ggplot components and as a byproduct gives you a gp
gglpot structure you can interogate via RStudio:
gp <- ggplot()
gp <- gp + geom_col()
gp <- gp + geom_line()
# gp <- gp + scale_y_continuous()
gp
this achieves the same result as the end of line +
without the extra superfluous function call. As an alternative, you might consider using geom_blank()
if you have a large code base to update then the latter geom_blank()
may be an easier choice. (This assumes you still debugging or tweaking your plots)
ggplot() +
geom_col() +
geom_line() +
# scale_y_continuous() +
geom_blank()
my sense is this is a question of aesthetics. I am not a fan of trailing +
because of the potential for drop through to other code (line overrun). Such situations can create interesting and awkward debugging situations.
Upvotes: 0