Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatting to numbers output by bi_class_breaks() #35

Open
RickPack opened this issue Jun 13, 2022 · 17 comments
Open

Add formatting to numbers output by bi_class_breaks() #35

RickPack opened this issue Jun 13, 2022 · 17 comments
Assignees

Comments

@RickPack
Copy link
Contributor

RickPack commented Jun 13, 2022

Might you enable the passing of functions to bi_class_breaks() to format numbers?

Or, for my particular interest, you could add an SI_lab (International System of Units) parameter that would pass digits to the si_number function in my code below.

This reduction of text for large numbers makes it easier to increase the font size and position the legend.

Currently

image

With proposed SI_lab parameter

Using SI_lab(3)
image

Code to aid addition of SI_lab parameter

First, create miami_rent_income object using my code at https://stackoverflow.com/a/72591940/2305061.

Then, focusing on modification to labels1.

miami_rent_income_bivar <-
  biscale::bi_class(miami_rent_income,
                    x = Median_Rent,
                    y = Median_Income, 
                    style = "jenks",
                    dim = 3)

labels1 <- bi_class_breaks(miami_rent_income, 
                           x = Median_Rent, 
                           y = Median_Income, 
                           style = "jenks", 
                           dim = 3, dig_lab = c(20,20), split = FALSE)

## function found at https://stackoverflow.com/a/59086755/2305061
si_number = function(x, digits) {
  
  compress = function(x, n) {
    signif(x * 10^(-n), digits)
  }
  
  case_when(
    ## decimals like 0.33
    abs(x) <    1   ~ as.character(x),   
    abs(x) >= 1e6   ~ paste0(compress(x, 6), "M"),
    abs(x) >= 1000  ~ paste0(compress(x, 3), "k"),
    abs(x) >= 1     ~ as.character(compress(x, 0)),
    abs(x) >= 0.001 ~ paste0(compress(x, -3), "m"),
    abs(x) >= 1e-6  ~ paste0(compress(x, -6), "u"),
    ## number of 0
    TRUE ~ as.character(x)
  )
}

labels1$bi_x <- paste0(si_number(as.numeric(str_extract(labels1$bi_x, ".+?(?=-)")), 3)
                       ,"-",
                       si_number(as.numeric(str_extract(labels1$bi_x, "(?<=\\-).*")), 3)
)
labels1$bi_y <- paste0(si_number(as.numeric(str_extract(labels1$bi_y, ".+?(?=-)")), 3)
                       ,"-",
                       si_number(as.numeric(str_extract(labels1$bi_y, "(?<=\\-).*")), 3)
)


miami_rent_income_bivar_legend <- bi_legend(pal = "DkCyan",
                                             breaks = labels1,
                                             xlab = "Median Rent ($)",
                                             ylab = "Median Income ($)",
                                             size = 08)
@RickPack
Copy link
Contributor Author

If you like my idea, I offer to attempt the addition of this SI_lab parameter to bi_class_breaks()

@chris-prener
Copy link
Owner

thanks for the suggestion @RickPack! I'd love to add a formatting argument for bi_class_braks(). I had thought about it for the v1.0 release last month but ran out of steam, so this is most welcome. Can you mirror the dig_lab functionality so that it takes either a scalar that is applied to both x and y or a vector where length(vector) == 2?

@RickPack
Copy link
Contributor Author

@chris-prener, I'll give it a try in the next few days!

@RickPack
Copy link
Contributor Author

Can you assign this to me?

@RickPack
Copy link
Contributor Author

I will get to this soon, @chris-prener, hopefully later today.

@chris-prener
Copy link
Owner

No rush @RickPack!

@RickPack
Copy link
Contributor Author

Working on this now!

@RickPack
Copy link
Contributor Author

@chris-prener, can you help me understand the need for as.numeric under the split == TRUE condition in https://github.com/chris-prener/biscale/blob/main/R/bi_class.R? My use of the SI prefix is causing an error (e.g., 323k instead of 323001).

@RickPack
Copy link
Contributor Author

@chris-prener also noting your title at https://chris-prener.github.io/biscale/articles/biscale.html has "Bivarite"

@RickPack
Copy link
Contributor Author

Along with the "Bivarite" misspelling, also need a library(ggplot2) somewhere before the ggplot() at https://chris-prener.github.io/biscale/articles/biscale.html.

chris-prener added a commit that referenced this issue Sep 15, 2022
This addresses the conversation in #35 - thanks @RickPack !
@chris-prener
Copy link
Owner

I'm going to leave this open just so that I can iron out some documentation for how this works, @RickPack - will ask you to take a look at that when it is done!

@RickPack
Copy link
Contributor Author

@chris-prener, will do and unfortunately, my technique occasionally does not work properly. I am on the run but will get you an example and hopefully a solution as soon as I can. It generally works though.

@RickPack
Copy link
Contributor Author

Example of a problem. Notice the "300m" label on the x-axis.
image

@quintercept
Copy link

Hi there! Been following this thread closely and excited you've been working on some formatting for the bi_class_breaks() labels. I wanted to see if there is any update or if this has been added to the newest version or if there's plans to released it yet? Thanks so much!

@chris-prener
Copy link
Owner

Hi @quintercept this is still in process!

@RickPack
Copy link
Contributor Author

RickPack commented Dec 5, 2022

@quintercept , you or another can edit my pull request if you like? I am not sure when I will get to this. Along with fixing the issue above, I would like to experiment with either label_number(scale_cut = cut_short_scale()) or label_number(scale_cut = cut_si("unit")) from the scales package.

@RickPack
Copy link
Contributor Author

RickPack commented Jan 15, 2023

Can anyone reproduce the problem I presented using the addition of an SI_lab parameter to bi_class_breaks() in my pull request? I have had a hard time doing so. I think it was for a year from 2018 - 2021, Census ACS5 data (default for the tidyverse R package), Fairfield County, Connecticut.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants