ggrepel: using position_dodge in combination with geom_label_repel
up vote
3
down vote
favorite
I'm trying to label the outliers in a geom_boxplot
using ggrepel::geom_label_repel
. It works nicely when there's only one grouping variable, but when I try it for multiple grouping variables I run into a problem. The position argument in ggrepel doesn't seem to work very consistently for some reason, see this example:
library(tidyverse)
library(ggrepel)
set.seed(1337)
df <- tibble(x = rnorm(500),
g1 = factor(sample(c('A','B'), 500, replace = TRUE)),
g2 = factor(sample(c('A','B'), 500, replace = TRUE)),
rownames = 1:500)
is_outlier <- function(x) {
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}
df_outliers <- df %>% group_by(g1, g2) %>% mutate(outlier=is_outlier(x))
ggplot(df_outliers, aes(x=g1, y=x, fill=g2)) +
geom_boxplot(width=0.3, position = position_dodge(0.5)) +
ggrepel::geom_label_repel(data=. %>% filter(outlier),
aes(label=rownames), position = position_dodge(0.8))
Is there a way to make the labels point to the accompanying dots using ggrepel?
r ggplot2 ggrepel
add a comment |
up vote
3
down vote
favorite
I'm trying to label the outliers in a geom_boxplot
using ggrepel::geom_label_repel
. It works nicely when there's only one grouping variable, but when I try it for multiple grouping variables I run into a problem. The position argument in ggrepel doesn't seem to work very consistently for some reason, see this example:
library(tidyverse)
library(ggrepel)
set.seed(1337)
df <- tibble(x = rnorm(500),
g1 = factor(sample(c('A','B'), 500, replace = TRUE)),
g2 = factor(sample(c('A','B'), 500, replace = TRUE)),
rownames = 1:500)
is_outlier <- function(x) {
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}
df_outliers <- df %>% group_by(g1, g2) %>% mutate(outlier=is_outlier(x))
ggplot(df_outliers, aes(x=g1, y=x, fill=g2)) +
geom_boxplot(width=0.3, position = position_dodge(0.5)) +
ggrepel::geom_label_repel(data=. %>% filter(outlier),
aes(label=rownames), position = position_dodge(0.8))
Is there a way to make the labels point to the accompanying dots using ggrepel?
r ggplot2 ggrepel
I think at least part of the problem has to do with having no B (g1) A (g2) outliers. You can get the dodging by adding that combination in,. %>% filter(outlier) %>% group_by(g1) %>% complete(g2)
. However, this doesn't fix the problem of how the lines are drawn with ggrepel.
– aosmith
Nov 19 at 22:35
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm trying to label the outliers in a geom_boxplot
using ggrepel::geom_label_repel
. It works nicely when there's only one grouping variable, but when I try it for multiple grouping variables I run into a problem. The position argument in ggrepel doesn't seem to work very consistently for some reason, see this example:
library(tidyverse)
library(ggrepel)
set.seed(1337)
df <- tibble(x = rnorm(500),
g1 = factor(sample(c('A','B'), 500, replace = TRUE)),
g2 = factor(sample(c('A','B'), 500, replace = TRUE)),
rownames = 1:500)
is_outlier <- function(x) {
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}
df_outliers <- df %>% group_by(g1, g2) %>% mutate(outlier=is_outlier(x))
ggplot(df_outliers, aes(x=g1, y=x, fill=g2)) +
geom_boxplot(width=0.3, position = position_dodge(0.5)) +
ggrepel::geom_label_repel(data=. %>% filter(outlier),
aes(label=rownames), position = position_dodge(0.8))
Is there a way to make the labels point to the accompanying dots using ggrepel?
r ggplot2 ggrepel
I'm trying to label the outliers in a geom_boxplot
using ggrepel::geom_label_repel
. It works nicely when there's only one grouping variable, but when I try it for multiple grouping variables I run into a problem. The position argument in ggrepel doesn't seem to work very consistently for some reason, see this example:
library(tidyverse)
library(ggrepel)
set.seed(1337)
df <- tibble(x = rnorm(500),
g1 = factor(sample(c('A','B'), 500, replace = TRUE)),
g2 = factor(sample(c('A','B'), 500, replace = TRUE)),
rownames = 1:500)
is_outlier <- function(x) {
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}
df_outliers <- df %>% group_by(g1, g2) %>% mutate(outlier=is_outlier(x))
ggplot(df_outliers, aes(x=g1, y=x, fill=g2)) +
geom_boxplot(width=0.3, position = position_dodge(0.5)) +
ggrepel::geom_label_repel(data=. %>% filter(outlier),
aes(label=rownames), position = position_dodge(0.8))
Is there a way to make the labels point to the accompanying dots using ggrepel?
r ggplot2 ggrepel
r ggplot2 ggrepel
edited Nov 19 at 15:02
Rui Barradas
14.8k31730
14.8k31730
asked Nov 19 at 14:58
Ravi
363
363
I think at least part of the problem has to do with having no B (g1) A (g2) outliers. You can get the dodging by adding that combination in,. %>% filter(outlier) %>% group_by(g1) %>% complete(g2)
. However, this doesn't fix the problem of how the lines are drawn with ggrepel.
– aosmith
Nov 19 at 22:35
add a comment |
I think at least part of the problem has to do with having no B (g1) A (g2) outliers. You can get the dodging by adding that combination in,. %>% filter(outlier) %>% group_by(g1) %>% complete(g2)
. However, this doesn't fix the problem of how the lines are drawn with ggrepel.
– aosmith
Nov 19 at 22:35
I think at least part of the problem has to do with having no B (g1) A (g2) outliers. You can get the dodging by adding that combination in,
. %>% filter(outlier) %>% group_by(g1) %>% complete(g2)
. However, this doesn't fix the problem of how the lines are drawn with ggrepel.– aosmith
Nov 19 at 22:35
I think at least part of the problem has to do with having no B (g1) A (g2) outliers. You can get the dodging by adding that combination in,
. %>% filter(outlier) %>% group_by(g1) %>% complete(g2)
. However, this doesn't fix the problem of how the lines are drawn with ggrepel.– aosmith
Nov 19 at 22:35
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377299%2fggrepel-using-position-dodge-in-combination-with-geom-label-repel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think at least part of the problem has to do with having no B (g1) A (g2) outliers. You can get the dodging by adding that combination in,
. %>% filter(outlier) %>% group_by(g1) %>% complete(g2)
. However, this doesn't fix the problem of how the lines are drawn with ggrepel.– aosmith
Nov 19 at 22:35