Skip to content

Commit 79257be

Browse files
committed
coordinate fixed range from data
1 parent ba738b9 commit 79257be

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

R/ggplotly.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ gg2list <- function(p) {
167167
range.values <- if(use.ranges){
168168
range.name <- paste0(xy, ".range")
169169
sapply(built$panel$ranges, "[[", range.name)
170-
}else{
170+
} else{
171171
## for categorical variables on the axes, panel$ranges info is
172172
## meaningless.
173173
name.name <- paste0(xy, ".name")
@@ -772,8 +772,8 @@ gg2list <- function(p) {
772772
# we make the size of the plot according to the specified ratio
773773
# note: we set the biggest dimension to 600
774774
if (!is.null(p$coordinates$ratio)) {
775-
x_range <- range(built[[2]]$ranges[[1]]$x.major_source, na.rm = TRUE)
776-
y_range <- range(built[[2]]$ranges[[1]]$y.major_source, na.rm = TRUE)
775+
x_range <- range(p$data[[as.character(layer.aes$x.name)]], na.rm = TRUE)
776+
y_range <- range(p$data[[as.character(layer.aes$y.name)]], na.rm = TRUE)
777777
yx_ratio <- (y_range[2] - y_range[1]) / (x_range[2] - x_range[1])
778778
if (yx_ratio > 1) {
779779
layout$height <- 600

tests/testthat/test-ggplot-coord.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ test_that("coord_fixed() is translated to the right height-width ratio", {
2828
la <- info$layout
2929
expect_identical(tr$type, "scatter")
3030
# height-width ratio check
31-
built <- ggplot_build2(p)
32-
x_range <- range(built[[2]]$ranges[[1]]$x.major_source, na.rm = TRUE)
33-
y_range <- range(built[[2]]$ranges[[1]]$y.major_source, na.rm = TRUE)
31+
x_range <- range(p$data$xval, na.rm = TRUE)
32+
y_range <- range(p$data$yval, na.rm = TRUE)
3433
yx_ratio <- (y_range[2] - y_range[1]) / (x_range[2] - x_range[1])
3534
expect_identical(la$height/la$width, yx_ratio * p$coordinates$ratio)
3635
})
@@ -45,8 +44,8 @@ test_that("coord_fixed() is translated to the right height-width ratio", {
4544
expect_identical(tr$type, "scatter")
4645
# height-width ratio check
4746
built <- ggplot_build2(p)
48-
x_range <- range(built[[2]]$ranges[[1]]$x.major_source, na.rm = TRUE)
49-
y_range <- range(built[[2]]$ranges[[1]]$y.major_source, na.rm = TRUE)
47+
x_range <- range(p$data$xval, na.rm = TRUE)
48+
y_range <- range(p$data$yval, na.rm = TRUE)
5049
yx_ratio <- (y_range[2] - y_range[1]) / (x_range[2] - x_range[1])
5150
expect_identical(la$height/la$width, yx_ratio * p$coordinates$ratio)
5251
})

0 commit comments

Comments
 (0)