Skip to content

Commit 1f9ef73

Browse files
authored
revert continuous hjust/vjust rotation; closes #2653
Revert continuous rotation of vjust and hjust parameters for arbitrary angles. Closes #2653.
1 parent 69dfc4b commit 1f9ef73

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

R/margins.R

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,35 @@ justify_grobs <- function(grobs, x = NULL, y = NULL, hjust = 0.5, vjust = 0.5,
297297
#'
298298
#' @noRd
299299
rotate_just <- function(angle, hjust, vjust) {
300-
# convert angle to radians
301-
rad <- (angle %||% 0) * pi / 180
302-
303-
hnew <- cos(rad) * hjust - sin(rad) * vjust + (1 - cos(rad) + sin(rad)) / 2
304-
vnew <- sin(rad) * hjust + cos(rad) * vjust + (1 - cos(rad) - sin(rad)) / 2
300+
## Ideally we would like to do something like the following commented-out lines,
301+
## but it currently yields unexpected results for angles other than 0, 90, 180, 270.
302+
## Problems arise in particular in cases where the horizontal and the vertical
303+
## alignment model differ, for example, where horizontal alignment is relative to a
304+
## point but vertical alignment is relative to an interval. This case arises for
305+
## x and y axis tick labels.
306+
##
307+
## For more details, see: https://github.com/tidyverse/ggplot2/issues/2653
308+
309+
# # convert angle to radians
310+
#rad <- (angle %||% 0) * pi / 180
311+
#
312+
#hnew <- cos(rad) * hjust - sin(rad) * vjust + (1 - cos(rad) + sin(rad)) / 2
313+
#vnew <- sin(rad) * hjust + cos(rad) * vjust + (1 - cos(rad) - sin(rad)) / 2
314+
315+
angle <- (angle %||% 0) %% 360
316+
if (0 <= angle & angle < 90) {
317+
hnew <- hjust
318+
vnew <- vjust
319+
} else if (90 <= angle & angle < 180) {
320+
hnew <- 1 - vjust
321+
vnew <- hjust
322+
} else if (180 <= angle & angle < 270) {
323+
hnew <- 1 - hjust
324+
vnew <- 1 - vjust
325+
} else if (270 <= angle & angle < 360) {
326+
hnew <- vjust
327+
vnew <- 1 - hjust
328+
}
305329

306330
list(hjust = hnew, vjust = vnew)
307331
}
Lines changed: 61 additions & 0 deletions
Loading

tests/testthat/test-theme.r

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,14 @@ test_that("strips can be styled independently", {
364364
)
365365
expect_doppelganger("strip_styling", plot)
366366
})
367+
368+
test_that("rotated axis tick labels work", {
369+
df <- data.frame(
370+
y = c(1, 2, 3),
371+
label = c("short", "medium size", "very long label")
372+
)
373+
374+
plot <- ggplot(df, aes(label, y)) + geom_point() +
375+
theme(axis.text.x = element_text(angle = 50, hjust = 1))
376+
expect_doppelganger("rotated x axis tick labels", plot)
377+
})

0 commit comments

Comments
 (0)