Skip to content

Commit

Permalink
revert continuous hjust/vjust rotation; closes #2653
Browse files Browse the repository at this point in the history
Revert continuous rotation of vjust and hjust parameters for arbitrary angles. Closes #2653.
  • Loading branch information
clauswilke authored May 24, 2018
1 parent 69dfc4b commit 1f9ef73
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
34 changes: 29 additions & 5 deletions R/margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,35 @@ justify_grobs <- function(grobs, x = NULL, y = NULL, hjust = 0.5, vjust = 0.5,
#'
#' @noRd
rotate_just <- function(angle, hjust, vjust) {
# convert angle to radians
rad <- (angle %||% 0) * pi / 180

hnew <- cos(rad) * hjust - sin(rad) * vjust + (1 - cos(rad) + sin(rad)) / 2
vnew <- sin(rad) * hjust + cos(rad) * vjust + (1 - cos(rad) - sin(rad)) / 2
## Ideally we would like to do something like the following commented-out lines,
## but it currently yields unexpected results for angles other than 0, 90, 180, 270.
## Problems arise in particular in cases where the horizontal and the vertical
## alignment model differ, for example, where horizontal alignment is relative to a
## point but vertical alignment is relative to an interval. This case arises for
## x and y axis tick labels.
##
## For more details, see: https://github.com/tidyverse/ggplot2/issues/2653

# # convert angle to radians
#rad <- (angle %||% 0) * pi / 180
#
#hnew <- cos(rad) * hjust - sin(rad) * vjust + (1 - cos(rad) + sin(rad)) / 2
#vnew <- sin(rad) * hjust + cos(rad) * vjust + (1 - cos(rad) - sin(rad)) / 2

angle <- (angle %||% 0) %% 360
if (0 <= angle & angle < 90) {
hnew <- hjust
vnew <- vjust
} else if (90 <= angle & angle < 180) {
hnew <- 1 - vjust
vnew <- hjust
} else if (180 <= angle & angle < 270) {
hnew <- 1 - hjust
vnew <- 1 - vjust
} else if (270 <= angle & angle < 360) {
hnew <- vjust
vnew <- 1 - hjust
}

list(hjust = hnew, vjust = vnew)
}
61 changes: 61 additions & 0 deletions tests/figs/themes/rotated-x-axis-tick-labels.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/testthat/test-theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,14 @@ test_that("strips can be styled independently", {
)
expect_doppelganger("strip_styling", plot)
})

test_that("rotated axis tick labels work", {
df <- data.frame(
y = c(1, 2, 3),
label = c("short", "medium size", "very long label")
)

plot <- ggplot(df, aes(label, y)) + geom_point() +
theme(axis.text.x = element_text(angle = 50, hjust = 1))
expect_doppelganger("rotated x axis tick labels", plot)
})

0 comments on commit 1f9ef73

Please sign in to comment.