Skip to content

Commit ef2f0e0

Browse files
type_text() retains categorical axes (#731)
* cat axis catch for type text * news * test
1 parent 26cadb2 commit ef2f0e0

4 files changed

Lines changed: 84 additions & 3 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ related to plot layering. See "Bug fixes" below.
214214

215215
### Bug fixes
216216

217+
- `type_text()` no longer converts a categorical axis to a numeric one.
218+
(#730 @grantmcdermott)
217219
- Fixed a bug where consecutive plots with (i) logged axes under (ii) a dynamic
218220
theme would error, due to a stale `par("xlog")`/`par("ylog")` state. We now
219221
avoid this by grabbing the log state directly from the top-level `log`

R/type_text.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,26 @@ data_text = function(labels = NULL, labeller = NULL, clim = c(0.5, 2.5)) {
166166
labels = tinylabel(labels, labeller)
167167
}
168168
datapoints$labels = labels
169+
# Collapse a categorical axis to its level positions, keeping the levels as
170+
# the axis labels. (#730)
169171
if (is.factor(datapoints$x)) {
170-
datapoints$x = as.numeric(datapoints$x)
172+
xlvls = levels(datapoints$x)
173+
xlabs = seq_along(xlvls)
174+
names(xlabs) = xlvls
175+
datapoints$x = as.integer(datapoints$x)
176+
} else {
177+
xlabs = NULL
171178
}
172179
if (is.factor(datapoints$y)) {
173-
datapoints$y = as.numeric(datapoints$y)
180+
ylvls = levels(datapoints$y)
181+
ylabs = seq_along(ylvls)
182+
names(ylabs) = ylvls
183+
datapoints$y = as.integer(datapoints$y)
184+
} else {
185+
ylabs = NULL
174186
}
175187

176-
env2env(environment(), settings, "datapoints")
188+
env2env(environment(), settings, c("datapoints", "xlabs", "ylabs"))
177189
}
178190
return(fun)
179191
}
Lines changed: 61 additions & 0 deletions
Loading

inst/tinytest/test-type_text.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,9 @@ expect_snapshot_plot(f, label = "text_labels_nse")
7171
expect_error(tinyplot(mpg ~ wt, data = mtcars, type = "text", labels = c("a", "b")))
7272
# top-level `labels` overrides the constructor-level arg
7373
expect_silent(tinyplot(1:3, type = type_text(labels = "x"), labels = c("a", "b", "c")))
74+
75+
# A categorical axis stays categorical (#730)
76+
f = function() {
77+
tinyplot(x = LETTERS[1:2], y = 1:2, type = "text")
78+
}
79+
expect_snapshot_plot(f, label = "text_categorical_x")

0 commit comments

Comments
 (0)