Skip to content

Commit bb8f960

Browse files
authored
Make annotate_logticks coord_flip-aware (#4431)
1 parent 6872bf6 commit bb8f960

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ggplot2 (development version)
22

3+
4+
* Fix bug in `annotate_logticks()` that would cause an error when used together
5+
with `coord_flip()` (@thomasp85, #3954)
6+
37
* Fix a bug in `guide_bins()` where keys would disappear if the guide was
48
reversed (@thomasp85, #4210)
59

R/annotation-logticks.r

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ GeomLogticks <- ggproto("GeomLogticks", Geom,
126126
mid = unit(0.2, "cm"), long = unit(0.3, "cm"))
127127
{
128128
ticks <- list()
129+
flipped <- inherits(coord, "CoordFlip")
130+
x_name <- if (flipped) "y" else "x"
131+
y_name <- if (flipped) "x" else "y"
129132

130133
# Convert these units to numbers so that they can be put in data frames
131134
short <- convertUnit(short, "cm", valueOnly = TRUE)
132135
mid <- convertUnit(mid, "cm", valueOnly = TRUE)
133136
long <- convertUnit(long, "cm", valueOnly = TRUE)
134137

135-
136138
if (grepl("[b|t]", sides)) {
137139

138140
# Get positions of x tick marks
@@ -149,7 +151,7 @@ GeomLogticks <- ggproto("GeomLogticks", Geom,
149151
if (scaled)
150152
xticks$value <- log(xticks$value, base)
151153

152-
names(xticks)[names(xticks) == "value"] <- "x" # Rename to 'x' for coordinates$transform
154+
names(xticks)[names(xticks) == "value"] <- x_name # Rename to 'x' for coordinates$transform
153155
xticks <- coord$transform(xticks, panel_params)
154156
xticks = xticks[xticks$x <= 1 & xticks$x >= 0,]
155157

@@ -173,7 +175,6 @@ GeomLogticks <- ggproto("GeomLogticks", Geom,
173175
}
174176
}
175177

176-
177178
if (grepl("[l|r]", sides)) {
178179
yticks <- calc_logticks(
179180
base = base,
@@ -188,7 +189,7 @@ GeomLogticks <- ggproto("GeomLogticks", Geom,
188189
if (scaled)
189190
yticks$value <- log(yticks$value, base)
190191

191-
names(yticks)[names(yticks) == "value"] <- "y" # Rename to 'y' for coordinates$transform
192+
names(yticks)[names(yticks) == "value"] <- y_name # Rename to 'y' for coordinates$transform
192193
yticks <- coord$transform(yticks, panel_params)
193194
yticks = yticks[yticks$y <= 1 & yticks$y >= 0,]
194195

0 commit comments

Comments
 (0)