ggplot2: How to reposition break lines next to bars in geom_bar instead of at the middle of the bar?












0















I have the following figure.



plot



As you can see the breaklines for the x scale (which is scaled discrete in this case) go right through the different bars. I want to these breaklines to form "lanes" in which the bars are nicely fitted. So instead of having my breaks go through the bar, I would like them to lie at the edge of each bar.



I experimented a lot with the scale_x_discrete function but just can't seem to figure out how to achieve this...










share|improve this question

























  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

    – Tung
    Nov 21 '18 at 13:26
















0















I have the following figure.



plot



As you can see the breaklines for the x scale (which is scaled discrete in this case) go right through the different bars. I want to these breaklines to form "lanes" in which the bars are nicely fitted. So instead of having my breaks go through the bar, I would like them to lie at the edge of each bar.



I experimented a lot with the scale_x_discrete function but just can't seem to figure out how to achieve this...










share|improve this question

























  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

    – Tung
    Nov 21 '18 at 13:26














0












0








0








I have the following figure.



plot



As you can see the breaklines for the x scale (which is scaled discrete in this case) go right through the different bars. I want to these breaklines to form "lanes" in which the bars are nicely fitted. So instead of having my breaks go through the bar, I would like them to lie at the edge of each bar.



I experimented a lot with the scale_x_discrete function but just can't seem to figure out how to achieve this...










share|improve this question
















I have the following figure.



plot



As you can see the breaklines for the x scale (which is scaled discrete in this case) go right through the different bars. I want to these breaklines to form "lanes" in which the bars are nicely fitted. So instead of having my breaks go through the bar, I would like them to lie at the edge of each bar.



I experimented a lot with the scale_x_discrete function but just can't seem to figure out how to achieve this...







r ggplot2 geom-bar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 13:16









Jordo82

59218




59218










asked Nov 21 '18 at 12:21









WBMWBM

194




194













  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

    – Tung
    Nov 21 '18 at 13:26



















  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

    – Tung
    Nov 21 '18 at 13:26

















Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

– Tung
Nov 21 '18 at 13:26





Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

– Tung
Nov 21 '18 at 13:26












1 Answer
1






active

oldest

votes


















1














You can create some fake gridlines using geom_vline while hiding the true gridlines.



library(tidyverse)

data <- data.frame(x = letters[1:10], y = runif(10), stringsAsFactors = FALSE)

data %>%
ggplot(aes(x, y)) +
geom_bar(stat = "identity") +
coord_flip() +
geom_vline(xintercept = seq(1.5, 9.5, 1), color = "white") +
theme(panel.grid = element_blank())


plot






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%2f53411920%2fggplot2-how-to-reposition-break-lines-next-to-bars-in-geom-bar-instead-of-at-th%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









    1














    You can create some fake gridlines using geom_vline while hiding the true gridlines.



    library(tidyverse)

    data <- data.frame(x = letters[1:10], y = runif(10), stringsAsFactors = FALSE)

    data %>%
    ggplot(aes(x, y)) +
    geom_bar(stat = "identity") +
    coord_flip() +
    geom_vline(xintercept = seq(1.5, 9.5, 1), color = "white") +
    theme(panel.grid = element_blank())


    plot






    share|improve this answer




























      1














      You can create some fake gridlines using geom_vline while hiding the true gridlines.



      library(tidyverse)

      data <- data.frame(x = letters[1:10], y = runif(10), stringsAsFactors = FALSE)

      data %>%
      ggplot(aes(x, y)) +
      geom_bar(stat = "identity") +
      coord_flip() +
      geom_vline(xintercept = seq(1.5, 9.5, 1), color = "white") +
      theme(panel.grid = element_blank())


      plot






      share|improve this answer


























        1












        1








        1







        You can create some fake gridlines using geom_vline while hiding the true gridlines.



        library(tidyverse)

        data <- data.frame(x = letters[1:10], y = runif(10), stringsAsFactors = FALSE)

        data %>%
        ggplot(aes(x, y)) +
        geom_bar(stat = "identity") +
        coord_flip() +
        geom_vline(xintercept = seq(1.5, 9.5, 1), color = "white") +
        theme(panel.grid = element_blank())


        plot






        share|improve this answer













        You can create some fake gridlines using geom_vline while hiding the true gridlines.



        library(tidyverse)

        data <- data.frame(x = letters[1:10], y = runif(10), stringsAsFactors = FALSE)

        data %>%
        ggplot(aes(x, y)) +
        geom_bar(stat = "identity") +
        coord_flip() +
        geom_vline(xintercept = seq(1.5, 9.5, 1), color = "white") +
        theme(panel.grid = element_blank())


        plot







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 13:31









        Jordo82Jordo82

        59218




        59218
































            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%2f53411920%2fggplot2-how-to-reposition-break-lines-next-to-bars-in-geom-bar-instead-of-at-th%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?