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