Skip to content

fixed problem with equal axes #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed

fixed problem with equal axes #223

wants to merge 14 commits into from

Conversation

13bzhang
Copy link
Contributor

A solution to Issue #218 "Equal scaling of axes not working" -- so that the height and width ratios will be those specified by users. Conformed to the style guide.

@cpsievert
Copy link
Collaborator

@13bzhang looking good!

I forgot to mention that when we add a new feature, we should add a test to demonstrate that it's working. In this case, I think starting a test-ggplot-coord.R file makes sense.

@13bzhang
Copy link
Contributor Author

Cool, I will add a test.

@13bzhang
Copy link
Contributor Author

I added a testthat test as Carson suggested.

@cpsievert @mkcor @chriddyp

@cpsievert
Copy link
Collaborator

Looks like the new tests aren't passing. There should be a little icon next to each commit to signify whether or not the build was successful. I'm having the same problem over at #217. Not sure why that is.

@13bzhang
Copy link
Contributor Author

Hi Carson, Sorry I'm stuck going through commencement weekend. I will take a look at this today.


# Data where x ranges from 0-10, y ranges from 0-30
set.seed(202)
dat <- data.frame(xval = runif(40,0,10), yval = runif(40,0,30))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember that we decided not to use randomized data in our test suite, so that the image diff's remain meaningful. But I may have totally missed a discussion on the topic in the meantime... If so, please let me know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that is still correct. I recommend just copying and pasting the results of the random data into the test :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. An example of this practice can be found in this test: https://github.com/ropensci/plotly/blob/master/tests/testthat/test-plotly-filename.R

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure set.seed() ensures we have the same set of numbers, so image diffs should be meaningful. That being said, I prefer setting the seed over copy/pasting numbers (I think it makes the tests easier to read).

Even better, use a built-in dataset such as mtcars, cars, iris, diamonds, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. set.seed() ensures we get the same data each time. I second Carson's point. I used the set.seed with the same seed as used in the R Cookbook. But I can use one of the datasets that come with R if it's preferred.

Gotta go to graduation now! Will be back shortly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @cpsievert and @13bzhang ! I have learned something today. :)
I agree, set.seed(1); runif(100) is definitely easier to read (and diff) than a sequence of 100 numbers...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkcor : Does that mean we can keep set.seed(22) as it was written in the codebook?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all good!

@13bzhang
Copy link
Contributor Author

@cpsievert
Hey Carson, I fixed the problem with my expect_traces function. I think that's what's failing the test. Oops my bad.

@cpsievert
Copy link
Collaborator

The message below was automatically generated after build https://travis-ci.org/ropensci/plotly/builds/63216395

On TravisCI, commit 31a1ef6 was successfully merged with 789e1ec (master) to create 147d8a4. A visual testing table comparing 789e1ec with 147d8a4 can be found here:
http://ropensci.github.io/plotly-test-table/tables/147d8a436687c219ba65c91630fbfafe14fe0942/index.html

y_range <- range(built[[2]]$ranges[[1]]$y.major_source, na.rm = TRUE)
yx_ratio <- (y_range[2] - y_range[1]) / (x_range[2] - x_range[1])
expect_identical(la$height/la$width, yx_ratio * p$coordinates$ratio)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@13bzhang Sorry, I must have clicked the wrong line: I meant "Please add new line at end of file" so we don't get this red sign from GitHub. Note that if you edit with vim, a new line at end of file is automatically added; in RStudio's editor, you need to actually add a line break after your last line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkcor
Got it! I added the space to the end of the test.

@cpsievert
Copy link
Collaborator

The message below was automatically generated after build https://travis-ci.org/ropensci/plotly/builds/63243339

On TravisCI, commit ba738b9 was successfully merged with 789e1ec (master) to create 62801e5. A visual testing table comparing 789e1ec with 62801e5 can be found here:
http://ropensci.github.io/plotly-test-table/tables/62801e5b243fdf48d6cc7ec83cea1ac0571dc794/index.html

@13bzhang
Copy link
Contributor Author

@cpsievert @mkcor @chriddyp
Is it ok to merge now?

@cpsievert
Copy link
Collaborator

The message below was automatically generated after build https://travis-ci.org/ropensci/plotly/builds/63413630

On TravisCI, commit 23eb47b was successfully merged with 789e1ec (master) to create ff6f23f. A visual testing table comparing 789e1ec with ff6f23f can be found here:
http://ropensci.github.io/plotly-test-table/tables/ff6f23feead39a52bfebe6f679510526792217c4/index.html

# height-width ratio check
built <- ggplot_build2(p)
x_range <- range(built[[2]]$ranges[[1]]$x.major_source, na.rm = TRUE)
y_range <- range(built[[2]]$ranges[[1]]$y.major_source, na.rm = TRUE)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should get the ranges directly from the data instead of via ggplot_build()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the range of the data itself be different from the ggplot_build() range if people decide to set their own xlim and ylim? I wanted to account for potential issues with that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, but you aren't setting xlim() or ylim() in the test itself, so you currently aren't testing for that...

In general, using the raw data as ground truth in tests is a good idea. Suppose ggplot2 internals change at some point and the x range is no longer stored in this location: built[[2]]$ranges[[1]]$x.major_source. This test wouldn't necessarily detect any problems since you're basically testing that we map the ggplot object in a certain way (which may be wrong).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Carson,
Good point. Ok, the changes should be made in ggplotly.R too righto?
@chriddyp @mkcor @chriddyp

@13bzhang 13bzhang force-pushed the baobao-equal_axes branch from 23eb47b to 955da5e Compare May 22, 2015 20:50
@13bzhang
Copy link
Contributor Author

Hi Carson, I changed the range to get it from the data instead of from ggplot_build2. See:
https://github.com/ropensci/plotly/blob/79257be14d48b2108fba4e09b2d8ce550740c404/R/ggplotly.R#L775-L776
Also I changed the test to reflect these changes.

@13bzhang
Copy link
Contributor Author

@cpsievert
Hi Carson, sorry for the late reply as I am traveling in California to work on a project with some folks at UC Berkeley. Yes, I understand what you're saying now. So I made ggplotly.R back to the way it was. For the test, I did the following:

test_that("coord_fixed() is translated to the right height-width ratio", {
  info <- expect_traces(p, 1, "force_equal_scaling")
  tr <- info$traces[[1]]
  la <- info$layout
  expect_identical(tr$type, "scatter")
  # height-width ratio check
  x_range <- range(p$data$xval, na.rm = TRUE)
  y_range <- range(p$data$yval, na.rm = TRUE)
  yx_ratio <- (y_range[2] - y_range[1]) / (x_range[2] - x_range[1])
  expect_identical(la$height/la$width, round(yx_ratio) * p$coordinates$ratio)
})

The range ratio from the raw data is going to be a bit off from the built ranges but the rounded ratio should be the same for each of the two plots in my test. Do you want me to implement an even more general test?
@mkcor @chriddyp

@cpsievert
Copy link
Collaborator

The message below was automatically generated after build https://travis-ci.org/ropensci/plotly/builds/64571974

On TravisCI, commit 5d9deca was successfully merged with fb40491 (master) to create a5355cd. A visual testing table comparing fb40491 with a5355cd can be found here:
http://ropensci.github.io/plotly-test-table/tables/a5355cd52f26ad079078aa6b404479c743080dfe/index.html

@cpsievert
Copy link
Collaborator

No worries @13bzhang, thanks for making those changes!

I think expect_equal() is a little more natural than expect_identical() when comparing quantities. identical() respects type (i.e., integer, numeric, etc), which isn't really a concern here, and with expect_equal() you can specify the tolerance (see 7cefa02 for an example)

@13bzhang
Copy link
Contributor Author

Thanks for the tip. I tried it with expect_equal and added tolerance. It worked pretty well. Will commit when I get wifi.

@13bzhang
Copy link
Contributor Author

13bzhang commented Jun 2, 2015

@cpsievert I added the parameter name tolerance and made the tolerance a bit smaller. Apparently the Travis CI didn't pass 3 days ago but it just passed now.

@cpsievert
Copy link
Collaborator

The message below was automatically generated after build https://travis-ci.org/ropensci/plotly/builds/65027665

On TravisCI, commit af4fd33 was successfully merged with fb40491 (master) to create 357c7c3. A visual testing table comparing fb40491 with 357c7c3 can be found here:
http://ropensci.github.io/plotly-test-table/tables/357c7c32de7f1db8d0898059b42568f14ddcfbc1/index.html

@cpsievert
Copy link
Collaborator

Ah, shoot, the test table didn't build since there were two builds happening at once. @13bzhang do you see the little "restart build" trigger on travis (for some reason I can't)? If so, please restart the build.

@13bzhang 13bzhang closed this Jun 2, 2015
@13bzhang 13bzhang reopened this Jun 2, 2015
@13bzhang
Copy link
Contributor Author

13bzhang commented Jun 2, 2015 via email

@13bzhang
Copy link
Contributor Author

13bzhang commented Jun 2, 2015

Here she blows! 🐳
I don't know if that was a really dumb way to restart the build.

} else {
layout$width <- 600
layout$height <- layout$height * (1 / p$coordinates$ratio) * yx_ratio
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coding the height/width is going to make for unpleasant htmlwidget experience. I think the best way to do it is pass this ratio to the HTMLwidget.resize method and impose the ratio on the height/width there. We should also provide a way to specify this ratio for non-ggplots

@cpsievert
Copy link
Collaborator

fixed axes were added in f9e3f2a

@cpsievert cpsievert closed this May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants