@@ -1136,23 +1136,30 @@ unitConvert <- function(u, to = c("npc", "pixels"), type = c("x", "y", "height",
1136
1136
# we need to know PPI/DPI of the display. I'm not sure of a decent way to do that
1137
1137
# from R, but it seems 96 is a reasonable assumption.
1138
1138
mm2pixels <- 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
+ unitType <- get(" unitType" , envir = asNamespace(" grid" ))
1142
+ if (unitType(u ) != " mm" ) {
1143
+ stop(" Unit must be in millimeters" )
1144
+ }
1145
+ } else {
1146
+ if (attr(u , " unit" ) != " mm" ) {
1147
+ stop(" Unit must be in millimeters" )
1148
+ }
1149
+ }
1150
+ (as.numeric(u ) * 96 ) / 25.4
1144
1151
}
1145
-
1152
+
1146
1153
verifyUnit <- 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" )
1154
+ # # the default unit in ggplot2 is millimeters (unless it's element_text())
1155
+ if (! grid :: is.unit(u )) {
1156
+ u <- if (inherits(u , " element" )) {
1157
+ grid :: unit(u $ size %|| % 0 , " points" )
1158
+ } else {
1159
+ grid :: unit(u %|| % 0 , " mm" )
1160
+ }
1153
1161
}
1154
- }
1155
- u
1162
+ u
1156
1163
}
1157
1164
1158
1165
# detect a blank theme element
0 commit comments