Skip to content

Commit ee2c285

Browse files
raviselkerjonathon-love
authored andcommitted
jmvbar: fix missing error bars bug
Previously, no error bars were shown when the continuous variable was named "y". This commit fixes that.
1 parent c87ba1b commit ee2c285

File tree

3 files changed

+95
-10
lines changed

3 files changed

+95
-10
lines changed

R/jmvbar.b.R

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ jmvbarClass <- if (requireNamespace("jmvcore", quietly = TRUE)) {
9797
if (is.null(group1)) {
9898
df <- self$data |>
9999
dplyr::mutate("{var}" := jmvcore::toNumeric(!!sym(var))) |>
100+
dplyr::rename(y_full = !!sym(var)) |>
100101
dplyr::summarize(
101-
y = mean(!!sym(var), na.rm = TRUE),
102+
y = mean(y_full, na.rm = TRUE),
102103
n = dplyr::n(),
103-
sd = sd(!!sym(var), na.rm = TRUE),
104+
sd = sd(y_full, na.rm = TRUE),
104105
) |>
105106
dplyr::mutate(se = sd / sqrt(n)) |>
106107
dplyr::mutate(ci = se * qt((self$options$ciWidth / 100) / 2 + .5, n - 1)) |>
@@ -109,28 +110,32 @@ jmvbarClass <- if (requireNamespace("jmvcore", quietly = TRUE)) {
109110
df <- self$data |>
110111
dplyr::mutate("{var}" := jmvcore::toNumeric(!!sym(var))) |>
111112
dplyr::group_by(!!sym(group1)) |>
113+
dplyr::rename(y_full = !!sym(var), x = !!sym(group1)) |>
112114
dplyr::summarize(
113-
y = mean(!!sym(var), na.rm = TRUE),
115+
y = mean(y_full, na.rm = TRUE),
114116
n = dplyr::n(),
115-
sd = sd(!!sym(var), na.rm = TRUE),
117+
sd = sd(y_full, na.rm = TRUE),
116118
) |>
117119
dplyr::mutate(se = sd / sqrt(n)) |>
118120
dplyr::mutate(ci = se * qt((self$options$ciWidth / 100) / 2 + .5, n - 1)) |>
119-
dplyr::ungroup() |>
120-
dplyr::rename(x = !!sym(group1))
121+
dplyr::ungroup()
121122
} else {
122123
df <- self$data |>
123124
dplyr::mutate("{var}" := jmvcore::toNumeric(!!sym(var))) |>
124125
dplyr::group_by(!!sym(group1), !!sym(group2)) |>
126+
dplyr::rename(
127+
y_full = !!sym(var),
128+
x = !!sym(group1),
129+
group = !!sym(group2)
130+
) |>
125131
dplyr::summarize(
126-
y = mean(!!sym(var), na.rm = TRUE),
132+
y = mean(y_full, na.rm = TRUE),
127133
n = dplyr::n(),
128-
sd = sd(!!sym(var), na.rm = TRUE),
134+
sd = sd(y_full, na.rm = TRUE),
129135
) |>
130136
dplyr::mutate(se = sd / sqrt(n)) |>
131137
dplyr::mutate(ci = se * qt((self$options$ciWidth / 100) / 2 + .5, n - 1)) |>
132-
dplyr::ungroup() |>
133-
dplyr::rename(x = !!sym(group1), group = !!sym(group2))
138+
dplyr::ungroup()
134139
}
135140

136141
return(df)
Lines changed: 57 additions & 0 deletions
Loading

tests/testthat/testbar.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ testthat::test_that("jmvbar: manual limits do not remove bars", {
252252
# THEN the bar should still be drawn (cropped)
253253
vdiffr::expect_doppelganger("jmvbar-manual-limits-zoom", disp_bar_zoom)
254254
})
255+
255256
#' Font Face Tests
256257
testthat::test_that("jmvbar: custom font faces, sizes, and alignment", {
257258
# GIVEN categorical data with custom font faces, sizes, and alignment
@@ -285,3 +286,25 @@ testthat::test_that("jmvbar: custom font faces, sizes, and alignment", {
285286
# THEN the plot should match the snapshot
286287
vdiffr::expect_doppelganger("jmvbar-custom-styling", disp_bar_jmvplot)
287288
})
289+
290+
#' Bar plot shows SEs
291+
testthat::test_that("jmvbar: SEs are shown (regression test)", {
292+
# GIVEN grouped continuous data
293+
df <- data.frame(
294+
# The name "y" previously caused problems
295+
y = c(94, 59, 84, 100, 61, 62, 30, 24, 11, 86),
296+
group = rep(c("Group_A", "Group_B"), each = 5)
297+
)
298+
299+
# WHEN a continuous bar plot with SEs is created
300+
disp_bar_ses <- scatr::jmvbar(
301+
data = df,
302+
mode = "continuous",
303+
convar = "y",
304+
congroup1 = "group",
305+
errorBars = "se"
306+
)
307+
308+
# THEN the plot should show SE bars and match the snapshot
309+
vdiffr::expect_doppelganger("jmvbar-ses", disp_bar_ses)
310+
})

0 commit comments

Comments
 (0)