class: center, middle, inverse, title-slide # scale_x_sd ## building a new scale like scale_x_log10 ### Gina Reynolds --- count: false .panel1-my_cars-auto[ ```r *ggplot2::scale_x_log10 ``` ] .panel2-my_cars-auto[ ``` function (...) { scale_x_continuous(..., trans = log10_trans()) } <bytecode: 0x7f88dfe81fe8> <environment: namespace:ggplot2> ``` ] <style> .panel1-my_cars-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-my_cars-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-my_cars-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-2-auto[ ```r *scales::log10_trans ``` ] .panel2-2-auto[ ``` function () { log_trans(10) } <bytecode: 0x7f88e0715a08> <environment: namespace:scales> ``` ] <style> .panel1-2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-3-auto[ ```r *scales::log_trans ``` ] .panel2-3-auto[ ``` function (base = exp(1)) { force(base) trans <- function(x) log(x, base) inv <- function(x) base^x trans_new(paste0("log-", format(base)), trans, inv, log_breaks(base = base), domain = c(1e-100, Inf)) } <bytecode: 0x7f88e07ef3b0> <environment: namespace:scales> ``` ] --- count: false .panel1-3-auto[ ```r scales::log_trans *scales::log_trans(base = exp(1)) ``` ] .panel2-3-auto[ ``` function (base = exp(1)) { force(base) trans <- function(x) log(x, base) inv <- function(x) base^x trans_new(paste0("log-", format(base)), trans, inv, log_breaks(base = base), domain = c(1e-100, Inf)) } <bytecode: 0x7f88e07ef3b0> <environment: namespace:scales> ``` ``` Transformer: log-2.718282 ``` ] <style> .panel1-3-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-3-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-3-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-4-auto[ ```r *scale_sd <- function(x){ * sdx <- sd(x) * meanx <- mean(x) * x/sdx - meanx/sdx *} ``` ] .panel2-4-auto[ ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } *library(scales) ``` ] .panel2-4-auto[ ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) *mine <- trans_new( * name = "mine", * transform = scale_sd, * inverse = scale_sd, * breaks = extended_breaks(5), * minor_breaks = regular_minor_breaks(), * format = format_format(), * domain = c(-Inf, Inf) *) ``` ] .panel2-4-auto[ ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) mine <- trans_new( name = "mine", transform = scale_sd, inverse = scale_sd, breaks = extended_breaks(5), minor_breaks = regular_minor_breaks(), format = format_format(), domain = c(-Inf, Inf) ) *1:10 %>% scale_sd() ``` ] .panel2-4-auto[ ``` [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446 [7] 0.4954337 0.8257228 1.1560120 1.4863011 ``` ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) mine <- trans_new( name = "mine", transform = scale_sd, inverse = scale_sd, breaks = extended_breaks(5), minor_breaks = regular_minor_breaks(), format = format_format(), domain = c(-Inf, Inf) ) 1:10 %>% scale_sd() *cars ``` ] .panel2-4-auto[ ``` [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446 [7] 0.4954337 0.8257228 1.1560120 1.4863011 ``` ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) mine <- trans_new( name = "mine", transform = scale_sd, inverse = scale_sd, breaks = extended_breaks(5), minor_breaks = regular_minor_breaks(), format = format_format(), domain = c(-Inf, Inf) ) 1:10 %>% scale_sd() cars %>% * ggplot()+ aes(speed, dist) ``` ] .panel2-4-auto[ ``` [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446 [7] 0.4954337 0.8257228 1.1560120 1.4863011 ``` ![](scale_x_sd_files/figure-html/4_auto_06_output-1.png)<!-- --> ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) mine <- trans_new( name = "mine", transform = scale_sd, inverse = scale_sd, breaks = extended_breaks(5), minor_breaks = regular_minor_breaks(), format = format_format(), domain = c(-Inf, Inf) ) 1:10 %>% scale_sd() cars %>% ggplot()+ aes(speed, dist) + * geom_point() ``` ] .panel2-4-auto[ ``` [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446 [7] 0.4954337 0.8257228 1.1560120 1.4863011 ``` ![](scale_x_sd_files/figure-html/4_auto_07_output-1.png)<!-- --> ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) mine <- trans_new( name = "mine", transform = scale_sd, inverse = scale_sd, breaks = extended_breaks(5), minor_breaks = regular_minor_breaks(), format = format_format(), domain = c(-Inf, Inf) ) 1:10 %>% scale_sd() cars %>% ggplot()+ aes(speed, dist) + geom_point() + * ggxmean:::geom_x1sd() ``` ] .panel2-4-auto[ ``` [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446 [7] 0.4954337 0.8257228 1.1560120 1.4863011 ``` ![](scale_x_sd_files/figure-html/4_auto_08_output-1.png)<!-- --> ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) mine <- trans_new( name = "mine", transform = scale_sd, inverse = scale_sd, breaks = extended_breaks(5), minor_breaks = regular_minor_breaks(), format = format_format(), domain = c(-Inf, Inf) ) 1:10 %>% scale_sd() cars %>% ggplot()+ aes(speed, dist) + geom_point() + ggxmean:::geom_x1sd() + * ggxmean::geom_x_mean() ``` ] .panel2-4-auto[ ``` [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446 [7] 0.4954337 0.8257228 1.1560120 1.4863011 ``` ![](scale_x_sd_files/figure-html/4_auto_09_output-1.png)<!-- --> ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) mine <- trans_new( name = "mine", transform = scale_sd, inverse = scale_sd, breaks = extended_breaks(5), minor_breaks = regular_minor_breaks(), format = format_format(), domain = c(-Inf, Inf) ) 1:10 %>% scale_sd() cars %>% ggplot()+ aes(speed, dist) + geom_point() + ggxmean:::geom_x1sd() + ggxmean::geom_x_mean() + * scale_x_continuous(trans = mine) ``` ] .panel2-4-auto[ ``` [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446 [7] 0.4954337 0.8257228 1.1560120 1.4863011 ``` ![](scale_x_sd_files/figure-html/4_auto_10_output-1.png)<!-- --> ] --- count: false .panel1-4-auto[ ```r scale_sd <- function(x){ sdx <- sd(x) meanx <- mean(x) x/sdx - meanx/sdx } library(scales) mine <- trans_new( name = "mine", transform = scale_sd, inverse = scale_sd, breaks = extended_breaks(5), minor_breaks = regular_minor_breaks(), format = format_format(), domain = c(-Inf, Inf) ) 1:10 %>% scale_sd() cars %>% ggplot()+ aes(speed, dist) + geom_point() + ggxmean:::geom_x1sd() + ggxmean::geom_x_mean() + scale_x_continuous(trans = mine) ``` ] .panel2-4-auto[ ``` [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446 [7] 0.4954337 0.8257228 1.1560120 1.4863011 ``` ![](scale_x_sd_files/figure-html/4_auto_11_output-1.png)<!-- --> ] <style> .panel1-4-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-4-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-4-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- <!-- adjust font size in this css code chunk, currently 80 --> <style type="text/css"> .remark-code{line-height: 1.5; font-size: 80%} @media print { .has-continuation { display: block; } } code.r.hljs.remark-code{ position: relative; overflow-x: hidden; } code.r.hljs.remark-code:hover{ overflow-x:visible; width: 500px; border-style: solid; } </style>