Skip to content

Commit eb9c080

Browse files
committed
merge conflicts
2 parents 193ba4e + d136b69 commit eb9c080

File tree

6 files changed

+56
-37
lines changed

6 files changed

+56
-37
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via 'plotly.js'
3-
Version: 3.5.4
3+
Version: 3.5.5
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
3.5.5 -- 5 May 2016
2+
3+
CHANGES:
4+
5+
ggplotly() will now use plotly's layout.axisid.title (instead of
6+
layout.annotations) for axis titles on non-faceted plots.
7+
This will make for a better title placement experience (see #510).
8+
9+
BUG FIX:
10+
11+
Space for interior facet_wrap() strips are now accounted for.
12+
113
3.5.4 -- 5 May 2016
214

315
BUG FIX:

R/ggplotly.R

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
300300
theme[["strip.text.x"]] %||% theme[["strip.text"]],
301301
"npc", "height"
302302
)
303+
panelMarginY <- panelMarginY + stripSize
303304
# space for ticks/text in free scales
304305
if (p$facet$free$x) {
305306
axisTicksX <- unitConvert(
@@ -330,7 +331,6 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
330331
rep(panelMarginX, 2),
331332
rep(panelMarginY, 2)
332333
)
333-
334334
doms <- get_domains(nPanels, nRows, margins)
335335

336336
for (i in seq_len(nPanels)) {
@@ -358,6 +358,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
358358
}
359359
# type of unit conversion
360360
type <- if (xy == "x") "height" else "width"
361+
# get axis title
362+
axisTitleText <- sc$name %||% p$labels[[xy]] %||% ""
363+
if (is_blank(axisTitle)) axisTitleText <- ""
361364
# https://plot.ly/r/reference/#layout-xaxis
362365
axisObj <- list(
363366
type = "linear",
@@ -373,7 +376,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
373376
ticklen = unitConvert(theme$axis.ticks.length, "pixels", type),
374377
tickwidth = unitConvert(axisTicks, "pixels", type),
375378
showticklabels = !is_blank(axisText),
376-
tickfont = text2font(axisText, "height"),
379+
tickfont = text2font(axisText, type),
377380
tickangle = - (axisText$angle %||% 0),
378381
showline = !is_blank(axisLine),
379382
linecolor = toRGB(axisLine$colour),
@@ -383,7 +386,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
383386
gridcolor = toRGB(panelGrid$colour),
384387
gridwidth = unitConvert(panelGrid, "pixels", type),
385388
zeroline = FALSE,
386-
anchor = anchor
389+
anchor = anchor,
390+
title = axisTitleText,
391+
titlefont = text2font(axisTitle)
387392
)
388393
# convert dates to milliseconds (86400000 = 24 * 60 * 60 * 1000)
389394
# this way both dates/datetimes are on same scale
@@ -403,27 +408,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
403408

404409
# do some stuff that should be done once for the entire plot
405410
if (i == 1) {
406-
# add space for exterior facet strips in `layout.margin`
407-
if (has_facet(p)) {
408-
stripSize <- unitConvert(stripText, "pixels", type)
409-
if (xy == "x") {
410-
gglayout$margin$t <- gglayout$margin$t + stripSize
411-
}
412-
if (xy == "y" && inherits(p$facet, "grid")) {
413-
gglayout$margin$r <- gglayout$margin$r + stripSize
414-
}
415-
}
416-
axisTitleText <- sc$name %||% p$labels[[xy]] %||% ""
417-
if (is_blank(axisTitle)) axisTitleText <- ""
418411
axisTickText <- axisObj$ticktext[which.max(nchar(axisObj$ticktext))]
419412
side <- if (xy == "x") "b" else "l"
420413
# account for axis ticks, ticks text, and titles in plot margins
421414
# (apparently ggplot2 doesn't support axis.title/axis.text margins)
422415
gglayout$margin[[side]] <- gglayout$margin[[side]] + axisObj$ticklen +
423416
bbox(axisTickText, axisObj$tickangle, axisObj$tickfont$size)[[type]] +
424417
bbox(axisTitleText, axisTitle$angle, unitConvert(axisTitle, "pixels", type))[[type]]
425-
# draw axis titles as annotations
426-
# (plotly.js axis titles aren't smart enough to dodge ticks & text)
418+
427419
if (nchar(axisTitleText) > 0) {
428420
axisTextSize <- unitConvert(axisText, "npc", type)
429421
axisTitleSize <- unitConvert(axisTitle, "npc", type)
@@ -432,22 +424,37 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
432424
bbox(axisTickText, axisText$angle, axisTextSize)[[type]] -
433425
bbox(axisTitleText, axisTitle$angle, axisTitleSize)[[type]] / 2 -
434426
unitConvert(theme$axis.ticks.length, "npc", type))
435-
# npc is on a 0-1 scale of the _entire_ device,
436-
# but these units _should_ be wrt to the plotting region
437-
# multiplying the offset by 2 seems to work, but this is a terrible hack
438-
offset <- 1.75 * offset
439-
x <- if (xy == "x") 0.5 else offset
440-
y <- if (xy == "x") offset else 0.5
441-
gglayout$annotations <- c(
442-
gglayout$annotations,
443-
make_label(
444-
faced(axisTitleText, axisTitle$face), x, y, el = axisTitle,
445-
xanchor = "center", yanchor = "middle", annotationType = "axis"
427+
}
428+
429+
# add space for exterior facet strips in `layout.margin`
430+
if (has_facet(p)) {
431+
stripSize <- unitConvert(stripText, "pixels", type)
432+
if (xy == "x") {
433+
gglayout$margin$t <- gglayout$margin$t + stripSize
434+
}
435+
if (xy == "y" && inherits(p$facet, "grid")) {
436+
gglayout$margin$r <- gglayout$margin$r + stripSize
437+
}
438+
# facets have multiple axis objects, but only one title for the plot,
439+
# so we empty the titles and try to draw the title as an annotation
440+
if (nchar(axisTitleText) > 0) {
441+
# npc is on a 0-1 scale of the _entire_ device,
442+
# but these units _should_ be wrt to the plotting region
443+
# multiplying the offset by 2 seems to work, but this is a terrible hack
444+
offset <- 1.75 * offset
445+
x <- if (xy == "x") 0.5 else offset
446+
y <- if (xy == "x") offset else 0.5
447+
gglayout$annotations <- c(
448+
gglayout$annotations,
449+
make_label(
450+
faced(axisTitleText, axisTitle$face), x, y, el = axisTitle,
451+
xanchor = "center", yanchor = "middle"
452+
)
446453
)
447-
)
454+
}
448455
}
449456
}
450-
457+
if (has_facet(p)) gglayout[[axisName]]$title <- ""
451458
} # end of axis loop
452459

453460
# theme(panel.border = ) -> plotly rect shape

tests/testthat/test-cookbook-axes.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ no.x.title <- bp +
107107

108108
test_that("coord_fixed(ratio)", {
109109
info <- expect_traces(no.x.title, 1, "no-x-title")
110-
expect_true(length(info$layout$annotations) == 1)
110+
expect_identical(info$layout$xaxis$title, "")
111111
})
112112

113113
# Also possible to set the axis label with the scale

tests/testthat/test-ggplot-labels.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ test_that("ylab is translated correctly", {
1313
geom_point(aes(Petal.Width, Sepal.Width)) +
1414
ylab("sepal width")
1515
info <- save_outputs(ggiris, "labels-ylab")
16-
labs <- unlist(lapply(info$layout$annotations, "[[", "text"))
17-
expect_identical(sort(labs), c("Petal.Width", "sepal width"))
16+
labs <- c(info$layout$xaxis$title, info$layout$yaxis$title)
17+
expect_identical(labs, c("Petal.Width", "sepal width"))
1818
})
1919

2020
# TODO: why is this failing on R-devel???

tests/testthat/test-ggplot-legend.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ test_that("Discrete colour and shape get merged into one legend", {
3030
nms, paste0("(", d$vs, ",", d$cyl, ")")
3131
)
3232
a <- info$layout$annotations
33-
expect_match(a[[3]]$text, "^factor\\(vs\\)")
34-
expect_match(a[[3]]$text, "factor\\(cyl\\)$")
35-
expect_true(a[[3]]$y > info$layout$legend$y)
33+
expect_match(a[[1]]$text, "^factor\\(vs\\)")
34+
expect_match(a[[1]]$text, "factor\\(cyl\\)$")
35+
expect_true(a[[1]]$y > info$layout$legend$y)
3636
})
3737

3838

0 commit comments

Comments
 (0)