Nick HK Profile picture
Econ prof @SeattleU. Book The Effect https://t.co/3F41M6EzEb out now! Check my pinned thread for all my projects. Substack https://t.co/YYrIUc8JPC

Aug 11, 2022, 7 tweets

scales, which formats numbers for presentation, is such an undersung package. I use it all the time. If you make anything in R that is intended for an audience to see - graphs, tables, RMarkdown/Quarto, it's perfect.

Neat scales functions, among others:
number() and comma(): format regular ol' numbers
comma(100000) becomes 100,000
number(1.324, accuracy = .1) becomes 1.3 (in a way that's much more reliable for this purpose than round())
number(1000, scale = 1/1000, suffix = 'k') becomes 1k

percent(.123) = 12.3%
dollar(123) = $123
alpha('red', .1) = "#FF00001A" (transparent color)
other handy color functions like grayscale palette grey_pal()

Plus the label_ functions for producing labels, like label_percent()/label_number() which are just versions of the above percent() and number(), etc., which can easily slot into ggplot2::scale_something_continuous(lables = ) since they return functions rather than values but ALSO

label_ordinal()(1) = "1st"
label_parse()('10^2') = expression(10^2) (for math in labels / titles)
label_wrap(10)('big ol long string') = 'big ol\nlong\nstring'
scales::label_date('%d/%m/%y')(as.Date('2020-01-02')) = '01/02/20'
(among many others)

How I use these all:
In Rmarkdown/Quarto:
The average was `r number(mean(dat$X), accuracy = .1)`, which made it the `label_ordinal()(which(ranklist == 'X')`-best option.
In making tables:
outputtable$Means = dollar(outputtable$meanincome)
outputtable
and of course in ggplot...

ggplot(data, aes(x = dollar(incomebins)) + geom_bar()
or
ggplot(data, aes(x = income, y = pct_employed) +
scale_x_continuous(labels = label_dollar())+
scale_y_continuous(labels = label_percent())

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling