Skip to content

Commit 6f8e2bf

Browse files
committed
Only merge legends if title and labels are the same
1 parent 686f705 commit 6f8e2bf

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ ggplot2 0.8.5 (2009-XX-XX) ----------------------------------------
3232
* scale_shape_discrete and scale_size_continuous added to scales to match
3333
naming convention of all other scales (Fixes #47)
3434
* scale_colour_gradient, scale_colour_gradient2 & scale_colour_gradientn now
35-
have formatter argument to match scale_continuous
35+
have formatter argument to match scale_continuous
36+
* legends will only merge if both the title and all labels are the same.
37+
(Fixes #16)

R/guides-legend.r

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ guide_legends <- function(scales, layers, default_mapping, theme) {
6464
legend <- scales$legend_desc(theme)
6565
if (length(legend$titles) == 0) return()
6666

67-
titles <- unique(legend$titles)
68-
lapply(titles, function(title) {
69-
keys <- legend$keys[sapply(legend$titles, identical, title)]
67+
hashes <- unique(legend$hash)
68+
lapply(hashes, function(hash) {
69+
keys <- legend$keys[legend$hash == hash]
70+
title <- legend$title[legend$hash == hash][[1]]
71+
7072
if (length(keys) > 1) {
7173
# Multiple scales for this legend
72-
keys <- merge_recurse(keys, by = c(".value", ".label"))
74+
keys <- merge_recurse(keys, by = ".label")
7375
} else {
7476
keys <- keys[[1]]
7577
}

R/scales-.r

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Scales <- proto(Scale, expr={
5656
legend_desc <- function(., theme) {
5757
# Loop through all scales, creating a list of titles, and a list of keys
5858
keys <- titles <- vector("list", .$n())
59+
hash <- character(.$n())
5960

6061
for(i in seq_len(.$n())) {
6162
scale <- .$.scales[[i]]
@@ -70,15 +71,16 @@ Scales <- proto(Scale, expr={
7071
}
7172

7273
key <- data.frame(
73-
scale$output_breaks(), scale$input_breaks(), I(scale$labels()))
74-
names(key) <- c(output, ".value", ".label")
74+
scale$output_breaks(), I(scale$labels()))
75+
names(key) <- c(output, ".label")
7576

7677
keys[[i]] <- key
78+
hash[i] <- digest(list(titles[[i]], key$.label))
7779
}
7880

7981
empty <- sapply(titles, is.null)
8082

81-
list(titles = titles[!empty], keys = keys[!empty])
83+
list(titles = titles[!empty], keys = keys[!empty], hash = hash[!empty])
8284

8385
}
8486

0 commit comments

Comments
 (0)