Skip to content

Commit b0510fe

Browse files
committed
Fix #240
1 parent e8238cf commit b0510fe

File tree

10 files changed

+285
-145
lines changed

10 files changed

+285
-145
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
* Fix a bug in `geom_autohistogram()` that prevented it to be used with
1414
continuous data (#297)
1515
* `facet_zoom()` now throws a better error when used with `coord_flip()` (#143)
16+
* You can now use `"inherit"`, `"inherit_fill"`, and `"inherit_col"` for the
17+
styling of the label box and connector in the `geom_mark_*()` family of geoms
18+
(#240)
1619

1720
# ggforce 0.4.1
1821

R/mark_circle.R

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,24 @@
7272
#' @param label.hjust The horizontal justification for the annotation. If it
7373
#' contains two elements the first will be used for the label and the second for
7474
#' the description.
75-
#' @param label.fill The fill colour for the annotation box.
75+
#' @param label.fill The fill colour for the annotation box. Use `"inherit"` to
76+
#' use the fill from the enclosure or `"inherit_col"` to use the border colour
77+
#' of the enclosure.
7678
#' @param label.colour The text colour for the annotation. If it contains
7779
#' two elements the first will be used for the label and the second for the
78-
#' description.
80+
#' description. Use `"inherit"` to use the border colour of the enclosure or
81+
#' `"inherit_fill"` to use the fill colour from the enclosure.
7982
#' @param label.buffer The size of the region around the mark where labels
8083
#' cannot be placed.
8184
#' @param con.colour The colour for the line connecting the annotation to the
82-
#' mark.
83-
#' @param con.size The width of the connector.
85+
#' mark. Use `"inherit"` to use the border colour of the enclosure or
86+
#' `"inherit_fill"` to use the fill colour from the enclosure.
87+
#' @param con.size The width of the connector. Use `"inherit"` to use the border
88+
#' width of the enclosure.
8489
#' @param con.type The type of the connector. Either `"elbow"`, `"straight"`, or
8590
#' `"none"`.
86-
#' @param con.linetype The linetype of the connector.
91+
#' @param con.linetype The linetype of the connector. Use `"inherit"` to use the
92+
#' border linetype of the enclosure.
8793
#' @param con.border The bordertype of the connector. Either `"one"` (to draw a
8894
#' line on the horizontal side closest to the mark), `"all"` (to draw a border
8995
#' on all sides), or `"none"` (not going to explain that one).
@@ -131,6 +137,14 @@
131137
#' geom_mark_circle(aes(fill = Species, label = Species),
132138
#' con.cap = 0) +
133139
#' geom_point()
140+
#'
141+
#' # If you want to use the scaled colours for the labels or connectors you can
142+
#' # use the "inherit" keyword instead
143+
#' ggplot(iris, aes(Petal.Length, Petal.Width)) +
144+
#' geom_mark_circle(aes(fill = Species, label = Species),
145+
#' label.fill = "inherit") +
146+
#' geom_point()
147+
#'
134148
NULL
135149

136150
#' @rdname ggforce-extensions
@@ -187,29 +201,42 @@ GeomMarkCircle <- ggproto('GeomMarkCircle', GeomShape,
187201
}
188202
}
189203

204+
gp <- gpar(
205+
col = first_rows$colour,
206+
fill = alpha(first_rows$fill, first_rows$alpha),
207+
lwd = (first_rows$linewidth %||% first_rows$size) * .pt,
208+
lty = first_rows$linetype,
209+
fontsize = (first_rows$size %||% 4.217518) * .pt
210+
)
211+
190212
circEncGrob(coords$x, coords$y,
191213
default.units = 'native',
192214
id = coords$group, expand = expand, radius = radius, n = n,
193215
label = label, ghosts = ghosts,
194-
mark.gp = gpar(
195-
col = first_rows$colour,
196-
fill = alpha(first_rows$fill, first_rows$alpha),
197-
lwd = (first_rows$linewidth %||% first_rows$size) * .pt,
198-
lty = first_rows$linetype
199-
),
200-
label.gp = gpar(
201-
col = label.colour,
216+
mark.gp = gp,
217+
label.gp = inherit_gp(
218+
col = label.colour[1],
202219
fill = label.fill,
203-
fontface = label.fontface,
204-
fontfamily = label.family,
205-
fontsize = label.fontsize,
206-
lineheight = label.lineheight
220+
fontface = label.fontface[1],
221+
fontfamily = label.family[1],
222+
fontsize = label.fontsize[1],
223+
lineheight = label.lineheight[1],
224+
gp = gp
225+
),
226+
desc.gp = inherit_gp(
227+
col = rep_len(label.colour, 2)[2],
228+
fontface = rep_len(label.fontface, 2)[2],
229+
fontfamily = rep_len(label.family, 2)[2],
230+
fontsize = rep_len(label.fontsize, 2)[2],
231+
lineheight = rep_len(label.lineheight, 2)[2],
232+
gp = gp
207233
),
208-
con.gp = gpar(
234+
con.gp = inherit_gp(
209235
col = con.colour,
210236
fill = con.colour,
211-
lwd = con.size * .pt,
212-
lty = con.linetype
237+
lwd = if (is.numeric(con.size)) con.size * .pt else con.size,
238+
lty = con.linetype,
239+
gp = gp
213240
),
214241
label.margin = label.margin,
215242
label.width = label.width,
@@ -297,7 +324,7 @@ geom_mark_circle <- function(mapping = NULL, data = NULL, stat = 'identity',
297324
circEncGrob <- function(x = c(0, 0.5, 1, 0.5), y = c(0.5, 1, 0.5, 0), id = NULL,
298325
id.lengths = NULL, expand = 0, radius = 0, n = 100,
299326
label = NULL, ghosts = NULL, default.units = 'npc',
300-
name = NULL, mark.gp = gpar(), label.gp = gpar(),
327+
name = NULL, mark.gp = gpar(), label.gp = gpar(), desc.gp = gpar(),
301328
con.gp = gpar(), label.margin = margin(),
302329
label.width = NULL, label.minwidth = unit(50, 'mm'),
303330
label.hjust = 0, label.buffer = unit(10, 'mm'),
@@ -335,10 +362,12 @@ circEncGrob <- function(x = c(0, 0.5, 1, 0.5), y = c(0.5, 1, 0.5, 0), id = NULL,
335362
label <- lapply(seq_len(nrow(label)), function(i) {
336363
if (is.na(label$label[i] %||% NA) && is.na(label$description[i] %||% NA)) return(zeroGrob())
337364
grob <- labelboxGrob(label$label[i], 0, 0, label$description[i],
338-
gp = label.gp, pad = label.margin, width = label.width,
365+
gp = subset_gp(label.gp, i), desc.gp = subset_gp(desc.gp, i),
366+
pad = label.margin, width = label.width,
339367
min.width = label.minwidth, hjust = label.hjust
340368
)
341369
if (con.border == 'all') {
370+
con.gp <- subset_gp(con.gp, i)
342371
grob$children[[1]]$gp$col <- con.gp$col
343372
grob$children[[1]]$gp$lwd <- con.gp$lwd
344373
grob$children[[1]]$gp$lty <- con.gp$lty

R/mark_ellipse.R

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@
7575
#' geom_mark_ellipse(aes(fill = Species, label = Species),
7676
#' con.cap = 0) +
7777
#' geom_point()
78+
#'
79+
#' # If you want to use the scaled colours for the labels or connectors you can
80+
#' # use the "inherit" keyword instead
81+
#' ggplot(iris, aes(Petal.Length, Petal.Width)) +
82+
#' geom_mark_ellipse(aes(fill = Species, label = Species),
83+
#' label.fill = "inherit") +
84+
#' geom_point()
7885
NULL
7986

8087
#' @rdname ggforce-extensions
@@ -123,29 +130,42 @@ GeomMarkEllipse <- ggproto('GeomMarkEllipse', GeomMarkCircle,
123130
}
124131
}
125132

133+
gp <- gpar(
134+
col = first_rows$colour,
135+
fill = alpha(first_rows$fill, first_rows$alpha),
136+
lwd = (first_rows$linewidth %||% first_rows$size) * .pt,
137+
lty = first_rows$linetype,
138+
fontsize = (first_rows$size %||% 4.217518) * .pt
139+
)
140+
126141
ellipEncGrob(coords$x, coords$y,
127142
default.units = 'native',
128143
id = coords$group, expand = expand, radius = radius, n = n,
129144
tol = tol, label = label, ghosts = ghosts,
130-
mark.gp = gpar(
131-
col = first_rows$colour,
132-
fill = alpha(first_rows$fill, first_rows$alpha),
133-
lwd = (first_rows$linewidth %||% first_rows$size) * .pt,
134-
lty = first_rows$linetype
135-
),
136-
label.gp = gpar(
137-
col = label.colour,
145+
mark.gp = gp,
146+
label.gp = inherit_gp(
147+
col = label.colour[1],
138148
fill = label.fill,
139-
fontface = label.fontface,
140-
fontfamily = label.family,
141-
fontsize = label.fontsize,
142-
lineheight = label.lineheight
149+
fontface = label.fontface[1],
150+
fontfamily = label.family[1],
151+
fontsize = label.fontsize[1],
152+
lineheight = label.lineheight[1],
153+
gp = gp
154+
),
155+
desc.gp = inherit_gp(
156+
col = rep_len(label.colour, 2)[2],
157+
fontface = rep_len(label.fontface, 2)[2],
158+
fontfamily = rep_len(label.family, 2)[2],
159+
fontsize = rep_len(label.fontsize, 2)[2],
160+
lineheight = rep_len(label.lineheight, 2)[2],
161+
gp = gp
143162
),
144-
con.gp = gpar(
163+
con.gp = inherit_gp(
145164
col = con.colour,
146165
fill = con.colour,
147-
lwd = con.size * .pt,
148-
lty = con.linetype
166+
lwd = if (is.numeric(con.size)) con.size * .pt else con.size,
167+
lty = con.linetype,
168+
gp = gp
149169
),
150170
label.margin = label.margin,
151171
label.width = label.width,
@@ -225,7 +245,7 @@ ellipEncGrob <- function(x = c(0, 0.5, 1, 0.5), y = c(0.5, 1, 0.5, 0), id = NULL
225245
id.lengths = NULL, expand = 0, radius = 0, n = 100,
226246
tol = 0.01, label = NULL, ghosts = NULL,
227247
default.units = 'npc', name = NULL, mark.gp = gpar(),
228-
label.gp = gpar(), con.gp = gpar(),
248+
label.gp = gpar(), desc.gp = gpar(), con.gp = gpar(),
229249
label.margin = margin(), label.width = NULL,
230250
label.minwidth = unit(50, 'mm'), label.hjust = 0,
231251
label.buffer = unit(10, 'mm'), con.type = 'elbow',
@@ -263,10 +283,12 @@ ellipEncGrob <- function(x = c(0, 0.5, 1, 0.5), y = c(0.5, 1, 0.5, 0), id = NULL
263283
label <- lapply(seq_len(nrow(label)), function(i) {
264284
if (is.na(label$label[i] %||% NA) && is.na(label$description[i] %||% NA)) return(zeroGrob())
265285
grob <- labelboxGrob(label$label[i], 0, 0, label$description[i],
266-
gp = label.gp, pad = label.margin, width = label.width,
286+
gp = subset_gp(label.gp, i), desc.gp = subset_gp(desc.gp, i),
287+
pad = label.margin, width = label.width,
267288
min.width = label.minwidth, hjust = label.hjust
268289
)
269290
if (con.border == 'all') {
291+
con.gp <- subset_gp(con.gp, i)
270292
grob$children[[1]]$gp$col <- con.gp$col
271293
grob$children[[1]]$gp$lwd <- con.gp$lwd
272294
grob$children[[1]]$gp$lty <- con.gp$lty

R/mark_hull.R

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@
8686
#' geom_mark_hull(aes(fill = Species, label = Species),
8787
#' con.cap = 0) +
8888
#' geom_point()
89+
#'
90+
#' # If you want to use the scaled colours for the labels or connectors you can
91+
#' # use the "inherit" keyword instead
92+
#' ggplot(iris, aes(Petal.Length, Petal.Width)) +
93+
#' geom_mark_hull(aes(fill = Species, label = Species),
94+
#' label.fill = "inherit") +
95+
#' geom_point()
96+
#'
8997
NULL
9098

9199
#' @rdname ggforce-extensions
@@ -134,30 +142,42 @@ GeomMarkHull <- ggproto('GeomMarkHull', GeomMarkCircle,
134142
}
135143
}
136144

145+
gp <- gpar(
146+
col = first_rows$colour,
147+
fill = alpha(first_rows$fill, first_rows$alpha),
148+
lwd = (first_rows$linewidth %||% first_rows$size) * .pt,
149+
lty = first_rows$linetype,
150+
fontsize = (first_rows$size %||% 4.217518) * .pt
151+
)
137152

138153
hullEncGrob(coords$x, coords$y,
139154
default.units = 'native',
140155
id = coords$group, expand = expand, radius = radius,
141156
concavity = concavity, label = label, ghosts = ghosts,
142-
mark.gp = gpar(
143-
col = first_rows$colour,
144-
fill = alpha(first_rows$fill, first_rows$alpha),
145-
lwd = (first_rows$linewidth %||% first_rows$size) * .pt,
146-
lty = first_rows$linetype
147-
),
148-
label.gp = gpar(
149-
col = label.colour,
157+
mark.gp = gp,
158+
label.gp = inherit_gp(
159+
col = label.colour[1],
150160
fill = label.fill,
151-
fontface = label.fontface,
152-
fontfamily = label.family,
153-
fontsize = label.fontsize,
154-
lineheight = label.lineheight
161+
fontface = label.fontface[1],
162+
fontfamily = label.family[1],
163+
fontsize = label.fontsize[1],
164+
lineheight = label.lineheight[1],
165+
gp = gp
166+
),
167+
desc.gp = inherit_gp(
168+
col = rep_len(label.colour, 2)[2],
169+
fontface = rep_len(label.fontface, 2)[2],
170+
fontfamily = rep_len(label.family, 2)[2],
171+
fontsize = rep_len(label.fontsize, 2)[2],
172+
lineheight = rep_len(label.lineheight, 2)[2],
173+
gp = gp
155174
),
156-
con.gp = gpar(
175+
con.gp = inherit_gp(
157176
col = con.colour,
158177
fill = con.colour,
159-
lwd = con.size * .pt,
160-
lty = con.linetype
178+
lwd = if (is.numeric(con.size)) con.size * .pt else con.size,
179+
lty = con.linetype,
180+
gp = gp
161181
),
162182
label.margin = label.margin,
163183
label.width = label.width,
@@ -232,7 +252,7 @@ hullEncGrob <- function(x = c(0, 0.5, 1, 0.5), y = c(0.5, 1, 0.5, 0), id = NULL,
232252
id.lengths = NULL, expand = 0, radius = 0, concavity = 2,
233253
label = NULL, ghosts = NULL, default.units = 'npc',
234254
name = NULL, mark.gp = gpar(), label.gp = gpar(),
235-
con.gp = gpar(), label.margin = margin(),
255+
desc.gp = gpar(), con.gp = gpar(), label.margin = margin(),
236256
label.width = NULL, label.minwidth = unit(50, 'mm'),
237257
label.hjust = 0, label.buffer = unit(10, 'mm'),
238258
con.type = 'elbow', con.border = 'one',
@@ -248,10 +268,12 @@ hullEncGrob <- function(x = c(0, 0.5, 1, 0.5), y = c(0.5, 1, 0.5, 0), id = NULL,
248268
label <- lapply(seq_len(nrow(label)), function(i) {
249269
if (is.na(label$label[i] %||% NA) && is.na(label$description[i] %||% NA)) return(zeroGrob())
250270
grob <- labelboxGrob(label$label[i], 0, 0, label$description[i],
251-
gp = label.gp, pad = label.margin, width = label.width,
271+
gp = subset_gp(label.gp, i), desc.gp = subset_gp(desc.gp, i),
272+
pad = label.margin, width = label.width,
252273
min.width = label.minwidth, hjust = label.hjust
253274
)
254275
if (con.border == 'all') {
276+
con.gp <- subset_gp(con.gp, i)
255277
grob$children[[1]]$gp$col <- con.gp$col
256278
grob$children[[1]]$gp$lwd <- con.gp$lwd
257279
grob$children[[1]]$gp$lty <- con.gp$lty

0 commit comments

Comments
 (0)