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

get_dims gives wrong dimensions when fixed dims >= max #10

Open
infotroph opened this issue Mar 5, 2016 · 0 comments
Open

get_dims gives wrong dimensions when fixed dims >= max #10

infotroph opened this issue Mar 5, 2016 · 0 comments
Labels

Comments

@infotroph
Copy link
Owner

If the fixed-size elements in a plot sum to a dimension great than maxheight or maxwidth, get_dims reports the wrong dimensions, possibly including negative values that produce errors from graphical devices:

p = ggplot(mtcars, aes(wt,mpg))+theme_ggEHD(96)
get_dims(p, 1, 1)
# $height
# [1] 1
# 
# $width
# [1] -0.4104287

ggsave_fitmax("test.png", p, 1, 1) # calls get_dims under the hood
# Error in grDevices::png(..., res = dpi, units = "in") : 
#  invalid quartz() device size

get_dims(p, 5, 5)
# $height
# [1] 5
# 
# $width
# [1] 4.922905
# ==> WRONG, this plot is supposed to be wider than it is tall!

ggsave_fitmax("test2.png", p, 5, 5)
# no error, but plot has wrong aspect ratio and is clipped with no warning

In this example, the reported width has no useful interpretation. The plot needs 6.50 inches of fixed height and 6.92 inches of fixed width, so get_dims subtracts those from 1 and divides the negative "remaining" space up between null units as min(1, 6.50 + ((1 - 6.92) * 0.75)) = 1 inches tall and min(1, 6.92 + ((1 - 6.50) / 0.75)) = -0.41 inches wide. Note especially that the "-0.41" cannot be interpreted as "the image needs to be at least 0.41 inches wider". It needs to be at least 5.92 inches wider just to start having room for the panel!

Probably best to fail loudly with something like

if(known_ht >= maxheight || known_wd >= maxwidth){
    stop("This plot will not fit in an image  smaller than",
        deparse(known_ht),  " x ", deparse(known_wd))}

Probably related to #9, in that both involve corner cases on image dimensions and ought to be considered at the same time.

@infotroph infotroph changed the title warn on negative get_dims output get_dims gives wrong dimensions when fixed dims >= max Mar 5, 2016
@infotroph infotroph added the bug label Mar 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant