0
votes

I have a boxplot graph where I want to show label names. The problem is that I want to repel these label names to make them beyond the boxplot. I tried geom_text_repel from ggrepel package but it repels lables when they overlap each other.

Also tried advice like this: Repel geom label and text in ggplot. And ordering geom points based on size

And have not received any comprehensive info about solving my problem.

Sample:

mtdata <- mtcars %>%
  rownames_to_column(var = "name") %>%
  mutate(cyl = as.factor(cyl))

ggplot(mtdata, aes(x = cyl, y = mpg)) + geom_boxplot() +
  geom_text_repel(data = mtdata %>%
               filter(mpg > 20 & wt >3), aes(label = name))

Desirable output:

enter image description here

So, you can see that there is a dot which depicts exact label position and repelling.

1

1 Answers

1
votes

A cheeky solution would be a simple nudge.

library(tidyverse)
mtdata <- mtcars %>%
  rownames_to_column(var = "name") %>%
  mutate(cyl = as.factor(cyl))

ggplot(mtdata, aes(x = cyl, y = mpg)) + geom_boxplot() +
  ggrepel::geom_text_repel(data = mtdata %>%
                    filter(mpg > 20 & wt >3), aes(label = name), nudge_x = .75)

Created on 2021-02-08 by the reprex package (v0.3.0)