ggrepel: using position_dodge in combination with geom_label_repel












4















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




















  • 1





    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 '18 at 22:35
















4















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




















  • 1





    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 '18 at 22:35














4












4








4








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 '18 at 15:02









Rui Barradas

16.5k51730




16.5k51730










asked Nov 19 '18 at 14:58









RaviRavi

413




413








  • 1





    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 '18 at 22:35














  • 1





    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 '18 at 22:35








1




1





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 '18 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 '18 at 22:35












1 Answer
1






active

oldest

votes


















0














You can try this:



ggplot(df_outliers, 
aes(x=g1, y=x, fill=g2, label=rownames)) +
geom_boxplot(width = 0.3, position = position_dodge(0.5)) +
geom_label_repel(data = . %>%
filter(outlier) %>%
group_by(g1) %>%
complete(g2, fill = list(x = 0, rownames = "")),
position = position_dodge(0.5),
box.padding = 1,
min.segment.length = 0,
show.legend = FALSE)


result



Explanations:




  1. The data source for geom_label_repel() follows aosmith's suggestion to add the B-A combination, filling 0 for x (any number would do, as long as it's not the default NA) and "" for rowname (ggrepel won't plot empty labels, but will take them into account when dodging).


  2. box.padding is set to 1 (increased from the default 0.25) to push the labels further away, so that the line segments are more visible.


  3. min.segment.length is set to 0 (decreased from the default 0.5) to force line segments to be plotted, no matter how short they are.



(show.legend = FALSE is optional. I just don't like seeing "a" letter show up in the legend.)






share|improve this answer























    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',
    autoActivateHeartbeat: false,
    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

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can try this:



    ggplot(df_outliers, 
    aes(x=g1, y=x, fill=g2, label=rownames)) +
    geom_boxplot(width = 0.3, position = position_dodge(0.5)) +
    geom_label_repel(data = . %>%
    filter(outlier) %>%
    group_by(g1) %>%
    complete(g2, fill = list(x = 0, rownames = "")),
    position = position_dodge(0.5),
    box.padding = 1,
    min.segment.length = 0,
    show.legend = FALSE)


    result



    Explanations:




    1. The data source for geom_label_repel() follows aosmith's suggestion to add the B-A combination, filling 0 for x (any number would do, as long as it's not the default NA) and "" for rowname (ggrepel won't plot empty labels, but will take them into account when dodging).


    2. box.padding is set to 1 (increased from the default 0.25) to push the labels further away, so that the line segments are more visible.


    3. min.segment.length is set to 0 (decreased from the default 0.5) to force line segments to be plotted, no matter how short they are.



    (show.legend = FALSE is optional. I just don't like seeing "a" letter show up in the legend.)






    share|improve this answer




























      0














      You can try this:



      ggplot(df_outliers, 
      aes(x=g1, y=x, fill=g2, label=rownames)) +
      geom_boxplot(width = 0.3, position = position_dodge(0.5)) +
      geom_label_repel(data = . %>%
      filter(outlier) %>%
      group_by(g1) %>%
      complete(g2, fill = list(x = 0, rownames = "")),
      position = position_dodge(0.5),
      box.padding = 1,
      min.segment.length = 0,
      show.legend = FALSE)


      result



      Explanations:




      1. The data source for geom_label_repel() follows aosmith's suggestion to add the B-A combination, filling 0 for x (any number would do, as long as it's not the default NA) and "" for rowname (ggrepel won't plot empty labels, but will take them into account when dodging).


      2. box.padding is set to 1 (increased from the default 0.25) to push the labels further away, so that the line segments are more visible.


      3. min.segment.length is set to 0 (decreased from the default 0.5) to force line segments to be plotted, no matter how short they are.



      (show.legend = FALSE is optional. I just don't like seeing "a" letter show up in the legend.)






      share|improve this answer


























        0












        0








        0







        You can try this:



        ggplot(df_outliers, 
        aes(x=g1, y=x, fill=g2, label=rownames)) +
        geom_boxplot(width = 0.3, position = position_dodge(0.5)) +
        geom_label_repel(data = . %>%
        filter(outlier) %>%
        group_by(g1) %>%
        complete(g2, fill = list(x = 0, rownames = "")),
        position = position_dodge(0.5),
        box.padding = 1,
        min.segment.length = 0,
        show.legend = FALSE)


        result



        Explanations:




        1. The data source for geom_label_repel() follows aosmith's suggestion to add the B-A combination, filling 0 for x (any number would do, as long as it's not the default NA) and "" for rowname (ggrepel won't plot empty labels, but will take them into account when dodging).


        2. box.padding is set to 1 (increased from the default 0.25) to push the labels further away, so that the line segments are more visible.


        3. min.segment.length is set to 0 (decreased from the default 0.5) to force line segments to be plotted, no matter how short they are.



        (show.legend = FALSE is optional. I just don't like seeing "a" letter show up in the legend.)






        share|improve this answer













        You can try this:



        ggplot(df_outliers, 
        aes(x=g1, y=x, fill=g2, label=rownames)) +
        geom_boxplot(width = 0.3, position = position_dodge(0.5)) +
        geom_label_repel(data = . %>%
        filter(outlier) %>%
        group_by(g1) %>%
        complete(g2, fill = list(x = 0, rownames = "")),
        position = position_dodge(0.5),
        box.padding = 1,
        min.segment.length = 0,
        show.legend = FALSE)


        result



        Explanations:




        1. The data source for geom_label_repel() follows aosmith's suggestion to add the B-A combination, filling 0 for x (any number would do, as long as it's not the default NA) and "" for rowname (ggrepel won't plot empty labels, but will take them into account when dodging).


        2. box.padding is set to 1 (increased from the default 0.25) to push the labels further away, so that the line segments are more visible.


        3. min.segment.length is set to 0 (decreased from the default 0.5) to force line segments to be plotted, no matter how short they are.



        (show.legend = FALSE is optional. I just don't like seeing "a" letter show up in the legend.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 16 at 6:30









        Z.LinZ.Lin

        10.9k21729




        10.9k21729






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?