Skip to content
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

Equal scaling of axes not working #218

Closed
13bzhang opened this issue May 13, 2015 · 3 comments
Closed

Equal scaling of axes not working #218

13bzhang opened this issue May 13, 2015 · 3 comments

Comments

@13bzhang
Copy link
Contributor

This is one of the unsupported features from the cookbook, see Issue #161.

There's a problem with setting the scaling of the axes to an equal ratio, with one visual unit being representing the same numeric unit on both axes. There's also a problem when I set them to ratios other than 1:1.

# 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)
)
sp <- ggplot(dat, aes(xval, yval)) + geom_point()

# Force equal scaling
sp + coord_fixed()

Cookbook:
1-1_scaling

Plotly:
1-1_scaling_plotly

# Equal scaling, with the total x axis the same length as the total y axis
sp + coord_fixed(ratio=1/3)

Cookbook:
1-3_scaling

Plotly:
1-3_scaling_plotly

cc @cpsievert @chriddyp @mkcor

@13bzhang
Copy link
Contributor Author

Here's a solution I came up with:

https://github.com/ropensci/plotly/blob/baobao-equal_axes/R/ggplotly.R#L771-785

  # fixed coordinates: if the coordinates ratio is not NULL, then
  # we make the size of the plot according to the specified ratio
  # note: we set the biggest dimension to 600
  if (!is.null(p$coordinates$ratio)){
    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)
    y.x.ratio <- (y.range[2] - y.range[1])/(x.range[2] - x.range[1])
    if (y.x.ratio > 1){
      layout$height <- 600
      layout$width <- layout$height*(1/p$coordinates$ratio)*(1/y.x.ratio) 
    } else{
      layout$width <- 600
      layout$height <- layout$height*(1/p$coordinates$ratio)*y.x.ratio 
    }
  }

What I did was if the user specified a coordinate ratio, the height and width in layout would be in that ratio. I was not sure how big the dimensions could get so I set the biggest dimension (whether it's x or y) to 600.

The new images look like this.

Cookbook:
1-1_scaling

Plotly:
new_1-1_scaling_plotly

Cookbook:
1-3_scaling

Plotly:
new_1-3_scaling_plotly

Questions, suggestions, comments welcome!
cc @cpsievert @chriddyp @mkcor

@chriddyp
Copy link
Member

that seems like the right approach!

This was referenced May 15, 2015
@cpsievert
Copy link
Collaborator

Closing in favor of #342

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

No branches or pull requests

3 participants