jolars / eulerr

Area-Proportional Euler and Venn Diagrams with Ellipses
https://jolars.github.io/eulerr/
GNU General Public License v3.0
130 stars 18 forks source link

Adding extra text/labels to Euler diagram. #70

Open JustGitting opened 4 years ago

JustGitting commented 4 years ago

Thanks for your work, it's a very useful package.

I'd like to add extra-text/labels to euler plots, so that I can provide summary information for each sub-group. Below is a mock-up of the idea.

euler_with_labels

I understand this would be non-trivial to automatically position the extra labels, but hope there is a work around.

Here is some example code on how to generate the sub group summary numbers (not used for the above mock-up image).

df <- data.frame( id = 1:12,
                  height = runif(12, min = 100, max = 150),
                  age = runif(12, min = 3, max = 50),
                  A = c(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),
                  B = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

group_lookup <- data.frame(A = c(0, 1, 1), 
                           B = c(1, 0, 1),
                           label = c('B', 'A', 'AnB'))

df <- df %>%
  left_join(group_lookup, by = c('A', 'B'))

group_summary <- df %>%
  group_by(label) %>%
  summarise(ave_age = mean(age),
            ave_height = mean(height))

# euler_data <- euler(c(A = 1, B = 4, "A&B" = 1))
euler_data <- euler(df[, c('A','B')])

p <- plot(euler_data)

# Add extra labels to plot or via eulerr functions?

Best wishes.

jw5 commented 3 years ago

Rather than attempting generic labeling inside the plot routine, perhaps the euler object could be augmented with an array of set intersection locations (centers?) for use adding labels to the plot afterward. Alternately, there could be a new helper function that took the euler object and returned an array/data_frame of labeled (sets intersected) and locations.

jw5 commented 3 years ago

It seems to me that there are two separate parts.

  1. Identifying the locations of the intersections (already done).
  2. Providing pretty user labels for selected intersections.

The current SW already does #1. #2 could be accomplished in a variety of ways. Either within or outside of the Euler diagram SW. One well accepted solution for labeling dense points with no label overlaps uses a physical simulation with the labels repelling each other. An example can be found here https://github.com/slowkow/ggrepel. This would provide a reasonable solution for automatic labeling within Euler. Alternately, a data frame of intersections and their locations could be returned and the user would be responsible for adding a new layer to the plot.

jolars commented 3 years ago

eulerr is by the way already using a modification of the ggrepel algorithm for overlapping labels, but it works so-so.