|> should always have a space before it, and should usually be followed by a new line. After the first step, each line should be indented by two spaces.
+ should always have a space before it, and should be followed by a new line. After the first step, each line should be indented by two spaces.
If you are creating a ggplot off of a dplyr pipeline, there should only be one level of indentation.
Good:
iris |>filter(Species =="setosa") |>ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +geom_point()
Bad:
iris |>filter(Species =="setosa") |>ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +geom_point()
Bad:
iris |>filter(Species =="setosa") |>ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +geom_point()
ggplot2 long lines
If the arguments to a ggplot2 layer don’t all fit on one line, put each argument on its own line and indent:
Good:
iris |>ggplot(aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +geom_point() +labs(x ="Sepal width, in cm",y ="Sepal length, in cm",title ="Sepal length vs. width of irises" )
Bad:
iris |>ggplot(aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +geom_point() +labs(x ="Sepal width, in cm", y ="Sepal length, in cm", title ="Sepal length vs. width of irises")
Code style summary
All code style guides are opinionated and subjective
Using consistent style makes it easier for collaborators (including future you!) to read and understand your code
Try to follow the tidyverse style guide in this class
A shortcut
In RStudio,
Highlight the code that you want to reformat
Go to “code –> reformat code”
Marvel in wonder
Try it
Reformat your code from an activity this week according to the tidyverse style guide