Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -1136,23 +1136,30 @@ unitConvert <- function(u, to = c("npc", "pixels"), type = c("x", "y", "height",
# we need to know PPI/DPI of the display. I'm not sure of a decent way to do that
# from R, but it seems 96 is a reasonable assumption.
mm2pixels <- function(u) {
u <- verifyUnit(u)
if (attr(u, "unit") != "mm") {
stop("Unit must be in millimeters")
}
(as.numeric(u) * 96) / 25.4
u <- verifyUnit(u)
if (getRversion() >= "4.0.0") {
unitType <- get("unitType", envir=asNamespace("grid"))
if (unitType(u) != "mm") {
stop("Unit must be in millimeters")
}
} else {
if (attr(u, "unit") != "mm") {
stop("Unit must be in millimeters")
}
}
(as.numeric(u) * 96) / 25.4
}

verifyUnit <- function(u) {
# the default unit in ggplot2 is millimeters (unless it's element_text())
if (is.null(attr(u, "unit"))) {
u <- if (inherits(u, "element")) {
grid::unit(u$size %||% 0, "points")
} else {
grid::unit(u %||% 0, "mm")
## the default unit in ggplot2 is millimeters (unless it's element_text())
if (!grid::is.unit(u)) {
u <- if (inherits(u, "element")) {
grid::unit(u$size %||% 0, "points")
} else {
grid::unit(u %||% 0, "mm")
}
}
}
u
u
}

# detect a blank theme element
Expand Down