Skip to content

Commit f7d5e68

Browse files
committed
Allow violins to scale freely.
Fixes #1388
1 parent 34d0bd5 commit f7d5e68

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ export(last_plot)
331331
export(layer)
332332
export(layer_data)
333333
export(layer_grob)
334+
export(layer_scales)
334335
export(lims)
335336
export(map_data)
336337
export(margin)

R/geom-violin.r

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ GeomViolin <- ggproto("GeomViolin", Geom,
9999

100100
# ymin, ymax, xmin, and xmax define the bounding rectangle for each group
101101
plyr::ddply(data, "group", transform,
102-
ymin = min(y),
103-
ymax = max(y),
104102
xmin = x - width / 2,
105103
xmax = x + width / 2
106104
)

R/plot-build.r

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#' \code{ggplot_build} takes the plot object, and performs all steps necessary
44
#' to produce an object that can be rendered. This function outputs two pieces:
55
#' a list of data frames (one for each layer), and a panel object, which
6-
#' contain all information about axis limits, breaks etc. \code{layer_data}
7-
#' and \code{layer_grob} are helper functions which returns the data or grob
8-
#' associated with a given layer.
6+
#' contain all information about axis limits, breaks etc.
7+
#'
8+
#' \code{layer_data}, \code{layer_grob}, and \code{layer_scales} are helper
9+
#' functions that returns the data, grob, or scales associated with a given
10+
#' layer. These are useful for tests.
911
#'
1012
#' @param plot ggplot object
1113
#' @seealso \code{\link{print.ggplot}} and \code{\link{benchplot}} for
@@ -95,6 +97,20 @@ layer_data <- function(plot, i = 1L) {
9597
ggplot_build(plot)$data[[i]]
9698
}
9799

100+
#' @export
101+
#' @rdname ggplot_build
102+
layer_scales <- function(plot, i = 1L, j = 1L) {
103+
b <- ggplot_build(plot)
104+
105+
layout <- b$panel$layout
106+
selected <- layout[layout$ROW == i & layout$COL == j, , drop = FALSE]
107+
108+
list(
109+
x = b$panel$x_scales[[selected$SCALE_X]],
110+
y = b$panel$y_scales[[selected$SCALE_Y]]
111+
)
112+
}
113+
98114
#' @export
99115
#' @rdname ggplot_build
100116
layer_grob <- function(plot, i = 1L) {

man/ggplot_build.Rd

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-geom-violin.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
context("geom_violin")
22

3+
test_that("", {
4+
df <- rbind(
5+
data.frame(x = "a", y = c(0, runif(10), 1)),
6+
data.frame(x = "b", y = c(0, runif(10), 2))
7+
)
8+
9+
p <- ggplot(df, aes(1, y)) +
10+
geom_violin() +
11+
facet_grid(x ~ ., scales = "free") +
12+
coord_cartesian(expand = FALSE)
13+
14+
expect_equal(layer_scales(p, 1)$y$dimension(), c(0, 1))
15+
expect_equal(layer_scales(p, 2)$y$dimension(), c(0, 2))
16+
})
17+
318
# create_quantile_segment_frame -------------------------------------------------
419

520
test_that("create_quantile_segment_frame functions for 3 quantiles", {

0 commit comments

Comments
 (0)