Aspect ratio

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.2.0
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
stdzd <- function(x) (x-mean(x))/sd(x)
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  scale_y_continuous(sec.axis = sec_axis(stdzd, name = "Standard Deviations")) + 
  scale_x_continuous(sec.axis = sec_axis(stdzd, name = "Standard Deviations"))

scale_y_sd <- function(){
  scale_y_continuous(sec.axis = sec_axis(stdzd, name = "Standard Deviations"))
  } 
scale_x_sd <- function(){
  scale_x_continuous(sec.axis = sec_axis(stdzd, name = "Standard Deviations")) 
  }


ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  scale_y_sd() + 
  scale_x_sd()

scale_xy_sd <- function(){
  
  list(scale_y_sd(),
    scale_x_sd())
  
}


last_plot() + 
  coord_fixed(ratio = sd(mtcars$wt)/sd(mtcars$mpg))

last_plot() + 
  ggxmean::geom_xy_means(shape = 4, size = 120, alpha = .3)

library(tidyverse)
mtcars |>
  ggplot() + 
  aes(x = wt, y = mpg) + 
  coord_fixed(ratio = sd(mtcars$wt)/sd(mtcars$mpg)) + 
  geom_point() + 
  geom_smooth(method = lm) + 
  geom_smooth(method = lm, formula = y ~ 1) + 
  ggxmean::geom_xy_means(shape = 4, size = 100, alpha = .5) +
  scale_y_sd() + 
  scale_x_sd() ->
p1; p1
## `geom_smooth()` using formula = 'y ~ x'

cars %>% 
  ggplot() + 
  aes(x = speed, y = dist) + 
  coord_fixed(ratio = sd(cars$speed)/sd(cars$dist)) + 
  geom_point() + 
  geom_smooth(method = lm) + 
  geom_smooth(method = lm, formula = y ~ 1) + 
  ggxmean::geom_xy_means(shape = 4, size = 100, alpha = .5)+
  scale_y_sd() + 
  scale_x_sd() ->
p2; p2
## `geom_smooth()` using formula = 'y ~ x'

corrr::correlate(cars)
## Correlation computed with
## • Method: 'pearson'
## • Missing treated using: 'pairwise.complete.obs'
## # A tibble: 2 × 3
##   term   speed   dist
##   <chr>  <dbl>  <dbl>
## 1 speed NA      0.807
## 2 dist   0.807 NA
mtcars %>% 
  select(wt, mpg) %>% 
  corrr:::correlate()
## Correlation computed with
## • Method: 'pearson'
## • Missing treated using: 'pairwise.complete.obs'
## # A tibble: 2 × 3
##   term      wt    mpg
##   <chr>  <dbl>  <dbl>
## 1 wt    NA     -0.868
## 2 mpg   -0.868 NA

library(patchwork)
(p1 + 
  scale_x_reverse() ) + p2
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

library(tidyverse)
library(palmerpenguins)
penguins %>% 
  filter(species == "Adelie") ->
adelie

adelie %>% 
  ggplot() + 
  aes(bill_length_mm, bill_depth_mm) + 
  geom_point()  + 
  geom_smooth(method = "lm") + 
  coord_fixed(ratio = sd(adelie$bill_length_mm, na.rm = T)/
                sd(adelie$bill_depth_mm, na.rm = T))
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).

palmerpenguins::penguins %>% 
  filter(species == "Gentoo") ->
gentoo

gentoo %>% 
  ggplot() + 
  aes(bill_length_mm, bill_depth_mm) + 
  geom_point() + 
  geom_smooth(method = "lm") + 
  coord_fixed(ratio = sd(gentoo$bill_length_mm, na.rm = T)/
                sd(gentoo$bill_depth_mm, na.rm = T))
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Removed 1 rows containing missing values (`geom_point()`).

# minimal example

theme_set(theme_gray(base_size = 18))
ggplot(mtcars, aes(x = wt, y = mpg)) +
  labs(title = "step 1. plot and 'square up' on sd") + 
  geom_point() + 
  geom_smooth(method = lm) + 
  coord_fixed(ratio = sd(mtcars$wt)/
                sd(mtcars$mpg))

last_plot() + 
  labs(title = "step 2. add 'empty model' for reference") +
  geom_smooth(method = lm, formula = y ~ 1, se = F) 

last_plot() +
  labs(title = "step 3. add slope logical limits\nw/ point shape '4' hack") +
  annotate(geom = "point", 
           x = mean(mtcars$wt),
           y = mean(mtcars$mpg),
           shape = 4, # 'x' shape
           size = 140, # make it giant
           alpha = .75  # a little transparent - it's reference
           )

library(tidyverse)
stdzd <- function(x) { (x-mean(x))/sd(x) }

mtcars0 <- mtcars %>% filter(!is.na(wt) & !is.na(mpg))

sd_wt <- sd(mtcars0$wt)
sd_mpg <- sd(mtcars0$mpg)
av_wt <- mean(mtcars0$wt)
av_mpg <- mean(mtcars$mpg)

ggplot(mtcars0, aes(x = wt, y = mpg)) +
  geom_point() +
  scale_y_continuous(breaks = mean(mtcars0$mpg) + (-3:3)*sd_mpg, sec.axis = sec_axis(stdzd, name = "Trying for 'Standard Deviations (Z)'")) + 
  scale_x_continuous(breaks = mean(mtcars0$wt) + (-3:3)*sd_wt , sec.axis = sec_axis(stdzd, name = "Trying for 'Standard Deviations (Z)'")) + 
  coord_fixed(ratio = sd(mtcars0$wt)/sd(mtcars0$mpg)) + 
  geom_smooth(method = lm) +
  annotate(geom = "point", 
           x = mean(mtcars0$wt),
           y = mean(mtcars0$mpg),
           shape = 4, # 'x' shape
           size = 140, # make it giant
           alpha = .75  # a little transparent - it's reference
           ) +
  geom_smooth(method = lm, formula = y ~ 1, se = F) 
## `geom_smooth()` using formula = 'y ~ x'

layer_data(last_plot())
## `geom_smooth()` using formula = 'y ~ x'
##        x    y PANEL group shape colour size fill alpha stroke
## 1  2.620 21.0     1    -1    19  black  1.5   NA    NA    0.5
## 2  2.875 21.0     1    -1    19  black  1.5   NA    NA    0.5
## 3  2.320 22.8     1    -1    19  black  1.5   NA    NA    0.5
## 4  3.215 21.4     1    -1    19  black  1.5   NA    NA    0.5
## 5  3.440 18.7     1    -1    19  black  1.5   NA    NA    0.5
## 6  3.460 18.1     1    -1    19  black  1.5   NA    NA    0.5
## 7  3.570 14.3     1    -1    19  black  1.5   NA    NA    0.5
## 8  3.190 24.4     1    -1    19  black  1.5   NA    NA    0.5
## 9  3.150 22.8     1    -1    19  black  1.5   NA    NA    0.5
## 10 3.440 19.2     1    -1    19  black  1.5   NA    NA    0.5
## 11 3.440 17.8     1    -1    19  black  1.5   NA    NA    0.5
## 12 4.070 16.4     1    -1    19  black  1.5   NA    NA    0.5
## 13 3.730 17.3     1    -1    19  black  1.5   NA    NA    0.5
## 14 3.780 15.2     1    -1    19  black  1.5   NA    NA    0.5
## 15 5.250 10.4     1    -1    19  black  1.5   NA    NA    0.5
## 16 5.424 10.4     1    -1    19  black  1.5   NA    NA    0.5
## 17 5.345 14.7     1    -1    19  black  1.5   NA    NA    0.5
## 18 2.200 32.4     1    -1    19  black  1.5   NA    NA    0.5
## 19 1.615 30.4     1    -1    19  black  1.5   NA    NA    0.5
## 20 1.835 33.9     1    -1    19  black  1.5   NA    NA    0.5
## 21 2.465 21.5     1    -1    19  black  1.5   NA    NA    0.5
## 22 3.520 15.5     1    -1    19  black  1.5   NA    NA    0.5
## 23 3.435 15.2     1    -1    19  black  1.5   NA    NA    0.5
## 24 3.840 13.3     1    -1    19  black  1.5   NA    NA    0.5
## 25 3.845 19.2     1    -1    19  black  1.5   NA    NA    0.5
## 26 1.935 27.3     1    -1    19  black  1.5   NA    NA    0.5
## 27 2.140 26.0     1    -1    19  black  1.5   NA    NA    0.5
## 28 1.513 30.4     1    -1    19  black  1.5   NA    NA    0.5
## 29 3.170 15.8     1    -1    19  black  1.5   NA    NA    0.5
## 30 2.770 19.7     1    -1    19  black  1.5   NA    NA    0.5
## 31 3.570 15.0     1    -1    19  black  1.5   NA    NA    0.5
## 32 2.780 21.4     1    -1    19  black  1.5   NA    NA    0.5
last_plot() -> g

g$scales
## <ggproto object: Class ScalesList, gg>
##     add: function
##     clone: function
##     find: function
##     get_scales: function
##     has_scale: function
##     input: function
##     n: function
##     non_position_scales: function
##     scales: list
##     super:  <ggproto object: Class ScalesList, gg>