Status Quo: do it with ggplot2
library(tidyverse)
palmerpenguins::penguins %>%
lm(bill_length_mm ~ species - 1, data = .) |>
# anova() | %>% >
summary() %>%
.$coefficients %>%
as.data.frame() |>
rownames_to_column("var") |>
ggplot() +
aes(x = Estimate - `Std. Error`) +
aes(xend = Estimate + `Std. Error`) +
aes(y = var %>% str_remove("species"), yend = var %>% str_remove("species")) +
geom_segment(color = "red") +
geom_point(aes(x = Estimate), color = "red") +
geom_point(shape = "|", data = palmerpenguins::penguins,
aes(y = species, x = bill_length_mm, xend = NULL, yend = NULL))
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
to geom
compute_panel_pm_se <- function(data, scales){
data$y <- as.factor(data$y)
data %>%
lm(x ~ y - 1, data = .) |>
summary() %>%
.$coefficients %>%
as.data.frame() |>
rownames_to_column("var") |>
mutate(x = Estimate - `Std. Error`) |>
mutate(xend = Estimate + `Std. Error`) |>
mutate(y = var %>% str_remove("y") %>% as.numeric() ,
yend = var %>% str_remove("y") %>% as.numeric() )
}
palmerpenguins::penguins |>
select(y = species, x = bill_length_mm) |>
compute_panel_pm_se()
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `y = var %>% str_remove("y") %>% as.numeric()`.
## Caused by warning in `var %>% str_remove("y") %>% as.numeric()`:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
## var Estimate Std. Error t value Pr(>|t|) x xend y
## 1 yAdelie 38.79139 0.2408694 161.0474 2.470328e-322 38.55052 39.03226 NA
## 2 yChinstrap 48.83382 0.3589349 136.0520 7.035960e-298 48.47489 49.19276 NA
## 3 yGentoo 47.50488 0.2668810 178.0002 0.000000e+00 47.23800 47.77176 NA
## yend
## 1 NA
## 2 NA
## 3 NA
ggtemp:::create_layer_temp("geom_plusminus_se",
compute_panel = compute_panel_pm_se,
geom_default = "segment")
palmerpenguins::penguins |>
ggplot() +
aes(x = flipper_length_mm, y = island) +
geom_point(alpha = .2, shape ="|") +
ggxmean::geom_xy_means(color = "red") +
geom_plusminus_se(color = "red")
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_xymean()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
last_plot() +
ggxmean::geom_x_mean(aes(y = NULL), linetype = "dashed")
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_xymean()`).
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_xmean()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Using the `size` aesthetic with geom_segment was deprecated in ggplot2 3.4.0.
## ℹ Please use the `linewidth` aesthetic instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
layer_data(i = 2)
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_xymean()`).
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_xmean()`).
## y x PANEL group shape colour size fill alpha stroke
## 1 1 209.7066 1 1 19 red 1.5 NA NA 0.5
## 2 2 193.0726 1 2 19 red 1.5 NA NA 0.5
## 3 3 191.1961 1 3 19 red 1.5 NA NA 0.5