Skip to content

Commit 981215e

Browse files
authored
Rescale sec.axis correctly for CoordPolar (#3278)
1 parent c4c540b commit 981215e

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ core developer team.
7171

7272
* `coord_map()` now can have axes on the top and right (@karawoo, #3042).
7373

74+
* `coord_polar()` now correctly rescales the secondary axis (@linzi-sg, #3278)
75+
7476
* `coord_sf()`, `coord_map()`, and `coord_polar()` now squash `-Inf` and `Inf`
7577
into the min and max of the plot (@yutannihilation, #2972).
7678

R/coord-polar.r

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ CoordPolar <- ggproto("CoordPolar", Coord,
160160
transform = function(self, data, panel_params) {
161161
data <- rename_data(self, data)
162162

163-
data$r <- r_rescale(self, data$r, panel_params)
163+
data$r <- r_rescale(self, data$r, panel_params$r.range)
164164
data$theta <- theta_rescale(self, data$theta, panel_params)
165165
data$x <- data$r * sin(data$theta) + 0.5
166166
data$y <- data$r * cos(data$theta) + 0.5
@@ -171,10 +171,14 @@ CoordPolar <- ggproto("CoordPolar", Coord,
171171
render_axis_v = function(self, panel_params, theme) {
172172
arrange <- panel_params$r.arrange %||% c("primary", "secondary")
173173

174-
x <- r_rescale(self, panel_params$r.major, panel_params) + 0.5
174+
x <- r_rescale(self, panel_params$r.major, panel_params$r.range) + 0.5
175175
panel_params$r.major <- x
176176
if (!is.null(panel_params$r.sec.major)) {
177-
panel_params$r.sec.major <- x
177+
panel_params$r.sec.major <- r_rescale(
178+
self,
179+
panel_params$r.sec.major,
180+
panel_params$r.sec.range
181+
) + 0.5
178182
}
179183

180184
list(
@@ -199,7 +203,7 @@ CoordPolar <- ggproto("CoordPolar", Coord,
199203
theta_rescale(self, panel_params$theta.minor, panel_params)
200204
thetafine <- seq(0, 2 * pi, length.out = 100)
201205

202-
rfine <- c(r_rescale(self, panel_params$r.major, panel_params), 0.45)
206+
rfine <- c(r_rescale(self, panel_params$r.major, panel_params$r.range), 0.45)
203207

204208
# This gets the proper theme element for theta and r grid lines:
205209
# panel.grid.major.x or .y
@@ -342,7 +346,7 @@ theta_rescale <- function(coord, x, panel_params) {
342346
rotate(rescale(x, c(0, 2 * pi), panel_params$theta.range))
343347
}
344348

345-
r_rescale <- function(coord, x, panel_params) {
346-
x <- squish_infinite(x, panel_params$r.range)
347-
rescale(x, c(0, 0.4), panel_params$r.range)
349+
r_rescale <- function(coord, x, range) {
350+
x <- squish_infinite(x, range)
351+
rescale(x, c(0, 0.4), range)
348352
}

0 commit comments

Comments
 (0)