Skip to content

Commit 34b224a

Browse files
authored
Compatibility current ggplot2 (#410)
* fix linetype scale * fix outdated alternative build functions * bump ggplot2 version * Revert "fix linetype scale" This reverts commit 99a1222. * Use `discrete_scale()` instead of `scale_linetype()`
1 parent acd4f10 commit 34b224a

File tree

4 files changed

+15
-79
lines changed

4 files changed

+15
-79
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Imports:
1717
ggforce,
1818
gghighlight,
1919
ggnewscale,
20-
ggplot2,
20+
ggplot2 (>= 3.5.0),
2121
ggraph,
2222
ggrepel,
2323
ggtext,

internals_ggbuild.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ggbuild <- function(plot) {
5050

5151

5252
# Transform all scales
53-
data <- lapply(data, ggplot2:::scales_transform_df, scales = scales) # ******
53+
data <- lapply(data, scales$transform_df) # ******
5454

5555
# Record the layer data after scale transformation applied
5656
all_steps$transformed <- data # ******
@@ -74,7 +74,7 @@ ggbuild <- function(plot) {
7474
all_steps$poststat <- data # ******
7575

7676
# Make sure missing (but required) aesthetics are added
77-
ggplot2:::scales_add_missing(plot, c("x", "y"), plot$plot_env) # ******
77+
scales$add_missing(c("x", "y"), plot$plot_env) # ******
7878

7979
# Reparameterise geoms from (e.g.) y and width to ymin and ymax
8080
data <- by_layer(function(l, d) l$compute_geom_1(d))
@@ -96,8 +96,8 @@ ggbuild <- function(plot) {
9696
# Train and map non-position scales
9797
npscales <- scales$non_position_scales()
9898
if (npscales$n() > 0) {
99-
lapply(data, ggplot2:::scales_train_df, scales = npscales) # ******
100-
data <- lapply(data, ggplot2:::scales_map_df, scales = npscales) # ******
99+
lapply(data, npscales$train_df) # ******
100+
data <- lapply(data, npscales$map_df) # ******
101101
}
102102

103103
# Fill in defaults etc.

internals_gggtable.R

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ gggtable <- function(data) {
1919
theme <- ggplot2:::plot_theme(plot) # ******
2020

2121
geom_grobs <- Map(function(l, d) l$draw_geom(d, layout), plot$layers, data)
22-
layout$setup_panel_guides(plot$guides, plot$layers, plot$mapping)
2322
plot_table <- layout$render(geom_grobs, data, theme, plot$labels)
2423

2524
# Record the state after the panel layouts have done their job (I think!)
@@ -32,75 +31,8 @@ gggtable <- function(data) {
3231
position <- "manual"
3332
}
3433

35-
legend_box <- if (position != "none") {
36-
ggplot2:::build_guides(plot$scales, plot$layers, plot$mapping, position, theme, plot$guides, plot$labels) # ******
37-
} else {
38-
zeroGrob()
39-
}
40-
41-
if (ggplot2:::is.zero(legend_box)) { # ******
42-
position <- "none"
43-
} else {
44-
# these are a bad hack, since it modifies the contents of viewpoint directly...
45-
legend_width <- gtable:::gtable_width(legend_box) # ******
46-
legend_height <- gtable:::gtable_height(legend_box) # ******
47-
48-
# Set the justification of the legend box
49-
# First value is xjust, second value is yjust
50-
just <- grid::valid.just(theme$legend.justification) # ******
51-
xjust <- just[1]
52-
yjust <- just[2]
53-
54-
if (position == "manual") {
55-
xpos <- theme$legend.position[1]
56-
ypos <- theme$legend.position[2]
57-
58-
# x and y are specified via theme$legend.position (i.e., coords)
59-
legend_box <- grid::editGrob(legend_box, # ******
60-
vp = grid::viewport(x = xpos, y = ypos, just = c(xjust, yjust), # ******
61-
height = legend_height, width = legend_width))
62-
} else {
63-
# x and y are adjusted using justification of legend box (i.e., theme$legend.justification)
64-
legend_box <- grid::editGrob(legend_box, # ******
65-
vp = grid::viewport(x = xjust, y = yjust, just = c(xjust, yjust))) # ******
66-
legend_box <- gtable:::gtable_add_rows(legend_box, unit(yjust, 'null')) # ******
67-
legend_box <- gtable:::gtable_add_rows(legend_box, unit(1 - yjust, 'null'), 0) # ******
68-
legend_box <- gtable:::gtable_add_cols(legend_box, unit(xjust, 'null'), 0) # ******
69-
legend_box <- gtable:::gtable_add_cols(legend_box, unit(1 - xjust, 'null')) # ******
70-
}
71-
}
72-
73-
panel_dim <- find_panel(plot_table)
74-
# for align-to-device, use this:
75-
# panel_dim <- summarise(plot_table$layout, t = min(t), r = max(r), b = max(b), l = min(l))
76-
77-
theme$legend.box.spacing <- theme$legend.box.spacing %||% unit(0.2, 'cm')
78-
if (position == "left") {
79-
plot_table <- gtable::gtable_add_cols(plot_table, theme$legend.box.spacing, pos = 0) # ******
80-
plot_table <- gtable::gtable_add_cols(plot_table, legend_width, pos = 0) # ******
81-
plot_table <- gtable::gtable_add_grob(plot_table, legend_box, clip = "off", # ******
82-
t = panel_dim$t, b = panel_dim$b, l = 1, r = 1, name = "guide-box")
83-
} else if (position == "right") {
84-
plot_table <- gtable::gtable_add_cols(plot_table, theme$legend.box.spacing, pos = -1) # ******
85-
plot_table <- gtable::gtable_add_cols(plot_table, legend_width, pos = -1) # ******
86-
plot_table <- gtable::gtable_add_grob(plot_table, legend_box, clip = "off", # ******
87-
t = panel_dim$t, b = panel_dim$b, l = -1, r = -1, name = "guide-box")
88-
} else if (position == "bottom") {
89-
plot_table <- gtable::gtable_add_rows(plot_table, theme$legend.box.spacing, pos = -1) # ******
90-
plot_table <- gtable::gtable_add_rows(plot_table, legend_height, pos = -1) # ******
91-
plot_table <- gtable::gtable_add_grob(plot_table, legend_box, clip = "off", # ******
92-
t = -1, b = -1, l = panel_dim$l, r = panel_dim$r, name = "guide-box")
93-
} else if (position == "top") {
94-
plot_table <- gtable::gtable_add_rows(plot_table, theme$legend.box.spacing, pos = 0) # ******
95-
plot_table <- gtable::gtable_add_rows(plot_table, legend_height, pos = 0) # ******
96-
plot_table <- gtable::gtable_add_grob(plot_table, legend_box, clip = "off", # ******
97-
t = 1, b = 1, l = panel_dim$l, r = panel_dim$r, name = "guide-box")
98-
} else if (position == "manual") {
99-
# should guide box expand whole region or region without margin?
100-
plot_table <- gtable::gtable_add_grob(plot_table, legend_box, # ******
101-
t = panel_dim$t, b = panel_dim$b, l = panel_dim$l, r = panel_dim$r,
102-
clip = "off", name = "guide-box")
103-
}
34+
legend_box <- plot$guides$assemble(theme)
35+
plot_table <- ggplot2:::table_add_legends(plot_table, legend_box, theme)
10436

10537
# Record the state of the gtable after the legends have been added
10638
all_states$legend <- plot_table # ******

scales-other.qmd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ base <- ggplot(df, aes(linetype = value)) +
245245
base
246246
```
247247

248-
You can control the line type by specifying a string with up to 8 hexadecimal values (i.e., from 0 to F). In this specification, the first value is the length of the first line segment, the second value is the length of the first space between segments, and so on. This allows you to specify your own line types using `scale_linetype_manual()`, or alternatively, by passing a custom function to the `palette` argument:
248+
You can control the line type by specifying a string with up to 8 hexadecimal values (i.e., from 0 to F).
249+
In this specification, the first value is the length of the first line segment, the second value is the length of the first space between segments, and so on.
250+
This allows you to specify your own line types using `scale_linetype_manual()`, or alternatively, by passing a custom function to the `palette` argument:
249251

250252
```{r}
251253
#| eval: false
@@ -264,14 +266,16 @@ linetypes <- function(n) {
264266
return(types[seq_len(n)])
265267
}
266268
267-
base + scale_linetype(palette = linetypes)
269+
base + discrete_scale("linetype", palette = linetypes)
268270
```
269271

270-
Note that the last four lines are blank, because the `linetypes()` function defined above returns `NA` when the number of categories exceeds 9. The `scale_linetype()` function contains a `na.value` argument used to specify what kind of line is plotted for these values. By default this produces a blank line, but you can override this by setting `na.value = "dotted"`:
272+
Note that the last four lines are blank, because the `linetypes()` function defined above returns `NA` when the number of categories exceeds 9.
273+
The `discrete_scale()` function contains a `na.value` argument used to specify what kind of line is plotted for these values.
274+
By default this produces a blank line, but you can override this by setting `na.value = "dotted"`:
271275

272276
```{r}
273277
#| eval: false
274-
base + scale_linetype(palette = linetypes, na.value = "dotted")
278+
base + discrete_scale("linetype", palette = linetypes)
275279
```
276280

277281
Valid line types can be set using a human readable character string: `"blank"`, `"solid"`, `"dashed"`, `"dotted"`, `"dotdash"`, `"longdash"`, and `"twodash"` are all understood.

0 commit comments

Comments
 (0)