@@ -127,7 +127,7 @@ ggplotly.ggmatrix <- function(p = ggplot2::last_plot(), width = NULL,
127
127
titleY = TRUE , titleX = TRUE ) %> %
128
128
hide_legend() %> %
129
129
layout(dragmode = " select" )
130
- if (nchar (p $ title %|| % " " ) > 0 ) {
130
+ if (robust_nchar (p $ title %|| % " " ) > 0 ) {
131
131
s <- layout(s , title = p $ title )
132
132
}
133
133
for (i in seq_along(p $ xAxisLabels )) {
@@ -436,7 +436,7 @@ gg2list <- function(p, width = NULL, height = NULL,
436
436
font = text2font(theme $ text )
437
437
)
438
438
# main plot title
439
- if (nchar (plot $ labels $ title %|| % " " ) > 0 ) {
439
+ if (robust_nchar (plot $ labels $ title %|| % " " ) > 0 ) {
440
440
gglayout $ title <- list (
441
441
text = faced(plot $ labels $ title , theme $ plot.title $ face ),
442
442
font = text2font(theme $ plot.title ),
@@ -567,7 +567,7 @@ gg2list <- function(p, width = NULL, height = NULL,
567
567
# allocate enough space for the _longest_ text label
568
568
axisTextX <- theme [[" axis.text.x" ]] %|| % theme [[" axis.text" ]]
569
569
labz <- unlist(lapply(layout $ panel_params , function (pp ) { pp [[" x" ]]$ get_labels %()% pp $ x.labels }))
570
- lab <- labz [which.max(nchar( labz ))]
570
+ lab <- longest_element( labz )
571
571
panelMarginY <- panelMarginY + axisTicksX +
572
572
bbox(lab , axisTextX $ angle , unitConvert(axisTextX , " npc" , " height" ))[[" height" ]]
573
573
}
@@ -579,7 +579,7 @@ gg2list <- function(p, width = NULL, height = NULL,
579
579
# allocate enough space for the _longest_ text label
580
580
axisTextY <- theme [[" axis.text.y" ]] %|| % theme [[" axis.text" ]]
581
581
labz <- unlist(lapply(layout $ panel_params , function (pp ) { pp [[" y" ]]$ get_labels %()% pp $ y.labels }))
582
- lab <- labz [which.max(nchar( labz ))]
582
+ lab <- longest_element( labz )
583
583
panelMarginX <- panelMarginX + axisTicksY +
584
584
bbox(lab , axisTextY $ angle , unitConvert(axisTextY , " npc" , " width" ))[[" width" ]]
585
585
}
@@ -806,15 +806,15 @@ gg2list <- function(p, width = NULL, height = NULL,
806
806
807
807
# do some stuff that should be done once for the entire plot
808
808
if (i == 1 ) {
809
- axisTickText <- axisObj $ ticktext [which.max(nchar( axisObj $ ticktext ))]
809
+ axisTickText <- longest_element( axisObj $ ticktext )
810
810
side <- if (xy == " x" ) " b" else " l"
811
811
# account for axis ticks, ticks text, and titles in plot margins
812
812
# (apparently ggplot2 doesn't support axis.title/axis.text margins)
813
813
gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] + axisObj $ ticklen +
814
814
bbox(axisTickText , axisObj $ tickangle , axisObj $ tickfont $ size )[[type ]] +
815
815
bbox(axisTitleText , axisTitle $ angle , unitConvert(axisTitle , " pixels" , type ))[[type ]]
816
816
817
- if (nchar (axisTitleText ) > 0 ) {
817
+ if (robust_nchar (axisTitleText ) > 0 ) {
818
818
axisTextSize <- unitConvert(axisText , " npc" , type )
819
819
axisTitleSize <- unitConvert(axisTitle , " npc" , type )
820
820
offset <-
@@ -836,7 +836,7 @@ gg2list <- function(p, width = NULL, height = NULL,
836
836
}
837
837
# facets have multiple axis objects, but only one title for the plot,
838
838
# so we empty the titles and try to draw the title as an annotation
839
- if (nchar (axisTitleText ) > 0 ) {
839
+ if (robust_nchar (axisTitleText ) > 0 ) {
840
840
# npc is on a 0-1 scale of the _entire_ device,
841
841
# but these units _should_ be wrt to the plotting region
842
842
# multiplying the offset by 2 seems to work, but this is a terrible hack
@@ -873,7 +873,7 @@ gg2list <- function(p, width = NULL, height = NULL,
873
873
)
874
874
if (is_blank(theme [[" strip.text.x" ]])) col_txt <- " "
875
875
if (inherits(plot $ facet , " FacetGrid" ) && lay $ ROW != 1 ) col_txt <- " "
876
- if (nchar (col_txt ) > 0 ) {
876
+ if (robust_nchar (col_txt ) > 0 ) {
877
877
col_lab <- make_label(
878
878
col_txt , x = mean(xdom ), y = max(ydom ),
879
879
el = theme [[" strip.text.x" ]] %|| % theme [[" strip.text" ]],
@@ -890,7 +890,7 @@ gg2list <- function(p, width = NULL, height = NULL,
890
890
)
891
891
if (is_blank(theme [[" strip.text.y" ]])) row_txt <- " "
892
892
if (inherits(plot $ facet , " FacetGrid" ) && lay $ COL != nCols ) row_txt <- " "
893
- if (nchar (row_txt ) > 0 ) {
893
+ if (robust_nchar (row_txt ) > 0 ) {
894
894
row_lab <- make_label(
895
895
row_txt , x = max(xdom ), y = mean(ydom ),
896
896
el = theme [[" strip.text.y" ]] %|| % theme [[" strip.text" ]],
@@ -1180,7 +1180,7 @@ is_blank <- function(x) {
1180
1180
# given text, and x/y coordinates on 0-1 scale,
1181
1181
# convert ggplot2::element_text() to plotly annotation
1182
1182
make_label <- function (txt = " " , x , y , el = ggplot2 :: element_text(), ... ) {
1183
- if (is_blank(el ) || is.null(txt ) || nchar (txt ) == 0 || length(txt ) == 0 ) {
1183
+ if (is_blank(el ) || is.null(txt ) || robust_nchar (txt ) == 0 || length(txt ) == 0 ) {
1184
1184
return (NULL )
1185
1185
}
1186
1186
angle <- el $ angle %|| % 0
@@ -1215,9 +1215,9 @@ has_facet <- function(x) {
1215
1215
1216
1216
bbox <- function (txt = " foo" , angle = 0 , size = 12 ) {
1217
1217
# assuming the horizontal size of a character is roughly half of the vertical
1218
- n <- nchar (txt )
1218
+ n <- robust_nchar (txt )
1219
1219
if (sum(n ) == 0 ) return (list (height = 0 , width = 0 ))
1220
- w <- size * (nchar (txt ) / 2 )
1220
+ w <- size * (robust_nchar (txt ) / 2 )
1221
1221
angle <- abs(angle %|| % 0 )
1222
1222
# do the sensible thing in the majority of cases
1223
1223
if (angle == 0 ) return (list (height = size , width = w ))
0 commit comments