@@ -1136,23 +1136,29 @@ unitConvert <- function(u, to = c("npc", "pixels"), type = c("x", "y", "height",
11361136# we need to know PPI/DPI of the display. I'm not sure of a decent way to do that
11371137# from R, but it seems 96 is a reasonable assumption.
11381138mm2pixels <- function (u ) {
1139- u <- verifyUnit(u )
1140- if (attr(u , " unit" ) != " mm" ) {
1141- stop(" Unit must be in millimeters" )
1142- }
1143- (as.numeric(u ) * 96 ) / 25.4
1139+ u <- verifyUnit(u )
1140+ if (getRversion() > = " 4.0.0" ) {
1141+ if (grid :: unitType(u ) != " mm" ) {
1142+ stop(" Unit must be in millimeters" )
1143+ }
1144+ } else {
1145+ if (attr(u , " unit" ) != " mm" ) {
1146+ stop(" Unit must be in millimeters" )
1147+ }
1148+ }
1149+ (as.numeric(u ) * 96 ) / 25.4
11441150}
1145-
1151+
11461152verifyUnit <- function (u ) {
1147- # the default unit in ggplot2 is millimeters (unless it's element_text())
1148- if (is.null(attr(u , " unit" ))) {
1149- u <- if (inherits(u , " element" )) {
1150- grid :: unit(u $ size %|| % 0 , " points" )
1151- } else {
1152- grid :: unit(u %|| % 0 , " mm" )
1153+ # # the default unit in ggplot2 is millimeters (unless it's element_text())
1154+ if (! grid :: is.unit(u )) {
1155+ u <- if (inherits(u , " element" )) {
1156+ grid :: unit(u $ size %|| % 0 , " points" )
1157+ } else {
1158+ grid :: unit(u %|| % 0 , " mm" )
1159+ }
11531160 }
1154- }
1155- u
1161+ u
11561162}
11571163
11581164# detect a blank theme element
0 commit comments