Reputation: 11
First time posting here... I have a Venn diagram where I have coded certain regions of intersections based on internally defined categories. I want to produce a legend that reflects those categories.
So far all I can find are Venn diagram legends that are populated based on the counts contained in the intersection regions. Although the counts are an important part of the Venn diagram, they are sufficiently represented in the figure with labels. Here's a coded example that produces a lengthy legend with each intersection listed (e.g., A, B, C, D, A..B, A..C, ...) to the right of the appropriately colored square, e.g., white, blue, or salmon. What I want is a succinct legend with just the three defined categories, one for each color with the text to the right specified as 'Exclude', 'Sample', and 'All'.
library(ggplot2)
library(ggVennDiagram)
x <- list(A = sample(1:50, 30), B = sample(10:60, 45), C = sample(1:100, 60), D = sample(1:50, 30))
v <- ggVennDiagram(x, label = 'count', label_alpha = 0) +
scale_color_manual(values = rep('black', 4))
v$layers[[1]]$mapping <- aes(fill = name)
Exclude <- 'white'
Sample <- 'skyblue'
All <- 'lightsalmon'
v + scale_fill_manual(values = c(A = Exclude, B = Exclude, C = Exclude, D = Exclude,
A..B = Exclude, A..C = Sample, A..D = All,
B..C = All, B..D = All, C..D = Exclude,
A..B..C = Sample, A..B..D = Sample, A..C..D = All,
B..C..D = All, A..B..C..D = Sample))
Upvotes: 1
Views: 111
Reputation: 125398
To achieve your desired result you have to recode the name
variable appropriatley for which I use dplyr::case_match
. Additionally, you have to map name
on the group
aes:
library(ggplot2)
library(ggVennDiagram)
set.seed(123)
x <- list(
A = sample(1:50, 30),
B = sample(10:60, 45),
C = sample(1:100, 60),
D = sample(1:50, 30)
)
v <- ggVennDiagram(x, label = "count", label_alpha = 0)
v$layers[[1]]$mapping <- aes(
group = .data$name,
fill = dplyr::case_match(
.data$name,
c("A", "B", "C", "D", "A/B", "C/D") ~ "Exclude",
c("A/C", "A/B/C", "A/B/D", "A/B/C/D") ~ "Sample",
.default = "All"
)
)
v +
scale_fill_manual(
values = c(
Exclude = "white",
Sample = "skyblue",
All = "lightsalmon"
)
)
Upvotes: 0