scales::breaks_pretty()
Function of the Week:
Ashley Blair
02/03/2022
Submission Instructions
Please sign up for a function here: https://docs.google.com/spreadsheets/d/1-RWAQTlLwttjFuZVAtSs8OiHIwu6AZLUdWugIHHTWVo/edit?usp=sharing
For this assignment, please submit both the .Rmd
and the .html
files. I will add it to the website. Remove your name from the Rmd if you do not wish it shared. If you select a function which was presented last year, please develop your own examples and content.
Breaks_Pretty
In this document, I will introduce the Breaks_Pretty function and show what it’s for.
#load tidyverse up
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.1.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
#example dataset
library(palmerpenguins)
#breaks pretty is in "scales" package
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
What is it for?
The breaks_pretty function is used to create easy, incremental break formatting - n= will specify the amount of breaks. This function is used more commonly for dates/times. Extended_breaks() is more commonly used for numeric scales. In the example below, I used numeric variables anyway.
#create plot & designate how many "breaks"
ggplot(penguins,
aes(x=bill_length_mm,
y=flipper_length_mm))+
geom_point() +
scale_x_continuous(name= "Bill Length in mm",
breaks = breaks_pretty(4))
## Warning: Removed 2 rows containing missing values (geom_point).
scale_y_continuous(name= "Flipper Length in mm",
breaks = breaks_pretty(8))
## <ScaleContinuousPosition>
## Range:
## Limits: 0 -- 1
#R doesn't always use the amount of "breaks" that you assign - apparently because it wants to best fit the data. For example - it won't allow for n=50 for y-axis.
ggplot(penguins,
aes(x=bill_length_mm,
y=flipper_length_mm))+
geom_point() +
scale_x_continuous(name= "Bill Length in mm",
breaks = breaks_pretty(2))
## Warning: Removed 2 rows containing missing values (geom_point).
scale_y_continuous(name= "Flipper Length in mm",
breaks = breaks_pretty(50))
## <ScaleContinuousPosition>
## Range:
## Limits: 0 -- 1
Here is an example using dates!
one_year <- as.POSIXct(c("2022-01-01", "2023-01-01"))
demo_datetime(one_year)
## scale_x_datetime()
demo_datetime(one_year, breaks = breaks_pretty(12))
## scale_x_datetime(breaks = breaks_pretty(12))
demo_datetime(one_year, breaks = breaks_pretty(6))
## scale_x_datetime(breaks = breaks_pretty(6))
demo_datetime(one_year, breaks = breaks_pretty(100))
## scale_x_datetime(breaks = breaks_pretty(100))
Is it helpful?
Discuss whether you think this function is useful for you and your work. Is it the best thing since sliced bread, or is it not really relevant to your work?
I find breaks_pretty much more useful in the dates example! Which makes sense considering it’s not generally used for numerical variables (penguin example).