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))


Resulting plot



Is there a way to make the labels point to the accompanying dots using ggrepel?










share|improve this question
























  • 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















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))


Resulting plot



Is there a way to make the labels point to the accompanying dots using ggrepel?










share|improve this question
























  • 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













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))


Resulting plot



Is there a way to make the labels point to the accompanying dots using ggrepel?










share|improve this question















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))


Resulting plot



Is there a way to make the labels point to the accompanying dots using ggrepel?







r ggplot2 ggrepel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python