How can I add a line below axis labels in ggplot2?












2















How can I add a line below axis labels just like in the attached picture where there is a line below 50,100 and 200 axis labels?
enter image description here










share|improve this question























  • stackoverflow.com/questions/12409960/… is one way to go, but using segmentsGrob & textGrobs but these things are a bit more esily done in base r as in meW answer

    – user20650
    Jan 2 at 11:53


















2















How can I add a line below axis labels just like in the attached picture where there is a line below 50,100 and 200 axis labels?
enter image description here










share|improve this question























  • stackoverflow.com/questions/12409960/… is one way to go, but using segmentsGrob & textGrobs but these things are a bit more esily done in base r as in meW answer

    – user20650
    Jan 2 at 11:53
















2












2








2








How can I add a line below axis labels just like in the attached picture where there is a line below 50,100 and 200 axis labels?
enter image description here










share|improve this question














How can I add a line below axis labels just like in the attached picture where there is a line below 50,100 and 200 axis labels?
enter image description here







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 10:15









M.R.WaniM.R.Wani

406




406













  • stackoverflow.com/questions/12409960/… is one way to go, but using segmentsGrob & textGrobs but these things are a bit more esily done in base r as in meW answer

    – user20650
    Jan 2 at 11:53





















  • stackoverflow.com/questions/12409960/… is one way to go, but using segmentsGrob & textGrobs but these things are a bit more esily done in base r as in meW answer

    – user20650
    Jan 2 at 11:53



















stackoverflow.com/questions/12409960/… is one way to go, but using segmentsGrob & textGrobs but these things are a bit more esily done in base r as in meW answer

– user20650
Jan 2 at 11:53







stackoverflow.com/questions/12409960/… is one way to go, but using segmentsGrob & textGrobs but these things are a bit more esily done in base r as in meW answer

– user20650
Jan 2 at 11:53














2 Answers
2






active

oldest

votes


















1














An example (doesn't contain GGPLOT2):



data("mtcars")
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution",
xlab="Number of Gears")
axis(1, # Put 1 for X-axis, 2 for Y-axis
at=c(0, 5), #Limit of line
col="red",
line=2.5, # how much gap you need between line and X-axis
labels=rep("",2), # remove line labels
lwd=2,
lwd.ticks=0) # remove ticks


plt



Multiple lines, just append another axis command as -



data("mtcars")
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution",
xlab="Number of Gears")
axis(1, # Put 1 for X-axis, 2 for Y-axis
at=c(0, 2.5), #Limit of line
col="red",
line=2.5, # how much gap you need between line and X-axis
labels=rep("",2), # remove line labels
lwd=2,
lwd.ticks=0) # remove ticks

axis(1, # Put 1 for X-axis, 2 for Y-axis
at=3+c(0, 2.5), #Limit of line
col="blue",
line=2.5, # how much gap you need between line and X-axis
labels=rep("",2), # remove line labels
lwd=2,
lwd.ticks=0) # remove ticks


plt2






share|improve this answer


























  • I get this error: Error in axis(1, at = c(0, 5), col = "red", line = 2.5, labels = rep("", : plot.new has not been called yet

    – M.R.Wani
    Jan 2 at 10:40











  • you need to call the plot command, which means plot the figure first then draw the line

    – meW
    Jan 2 at 10:41











  • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

    – M.R.Wani
    Jan 2 at 10:44













  • What do you mean by these variables? Elaborate.

    – meW
    Jan 2 at 10:45











  • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

    – M.R.Wani
    Jan 2 at 10:51



















0














Maybe the facet_wrap from ggplot2 is a starting point:





library(magrittr)
library(ggplot2)

mtcars %>%
dplyr::mutate(hp_200 = ifelse(hp > 200, "hp > 200", "hp <= 200")) %>%
ggplot(aes(x = hp)) +
geom_histogram(binwidth = 20) +
facet_wrap(~ hp_200, scales = "free_x", strip.position = "bottom") +
ggthemes::theme_hc()




Created on 2019-01-02 by the reprex package (v0.2.1)






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%2f54004510%2fhow-can-i-add-a-line-below-axis-labels-in-ggplot2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    An example (doesn't contain GGPLOT2):



    data("mtcars")
    counts <- table(mtcars$gear)
    barplot(counts, main="Car Distribution",
    xlab="Number of Gears")
    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=c(0, 5), #Limit of line
    col="red",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks


    plt



    Multiple lines, just append another axis command as -



    data("mtcars")
    counts <- table(mtcars$gear)
    barplot(counts, main="Car Distribution",
    xlab="Number of Gears")
    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=c(0, 2.5), #Limit of line
    col="red",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks

    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=3+c(0, 2.5), #Limit of line
    col="blue",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks


    plt2






    share|improve this answer


























    • I get this error: Error in axis(1, at = c(0, 5), col = "red", line = 2.5, labels = rep("", : plot.new has not been called yet

      – M.R.Wani
      Jan 2 at 10:40











    • you need to call the plot command, which means plot the figure first then draw the line

      – meW
      Jan 2 at 10:41











    • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

      – M.R.Wani
      Jan 2 at 10:44













    • What do you mean by these variables? Elaborate.

      – meW
      Jan 2 at 10:45











    • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

      – M.R.Wani
      Jan 2 at 10:51
















    1














    An example (doesn't contain GGPLOT2):



    data("mtcars")
    counts <- table(mtcars$gear)
    barplot(counts, main="Car Distribution",
    xlab="Number of Gears")
    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=c(0, 5), #Limit of line
    col="red",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks


    plt



    Multiple lines, just append another axis command as -



    data("mtcars")
    counts <- table(mtcars$gear)
    barplot(counts, main="Car Distribution",
    xlab="Number of Gears")
    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=c(0, 2.5), #Limit of line
    col="red",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks

    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=3+c(0, 2.5), #Limit of line
    col="blue",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks


    plt2






    share|improve this answer


























    • I get this error: Error in axis(1, at = c(0, 5), col = "red", line = 2.5, labels = rep("", : plot.new has not been called yet

      – M.R.Wani
      Jan 2 at 10:40











    • you need to call the plot command, which means plot the figure first then draw the line

      – meW
      Jan 2 at 10:41











    • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

      – M.R.Wani
      Jan 2 at 10:44













    • What do you mean by these variables? Elaborate.

      – meW
      Jan 2 at 10:45











    • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

      – M.R.Wani
      Jan 2 at 10:51














    1












    1








    1







    An example (doesn't contain GGPLOT2):



    data("mtcars")
    counts <- table(mtcars$gear)
    barplot(counts, main="Car Distribution",
    xlab="Number of Gears")
    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=c(0, 5), #Limit of line
    col="red",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks


    plt



    Multiple lines, just append another axis command as -



    data("mtcars")
    counts <- table(mtcars$gear)
    barplot(counts, main="Car Distribution",
    xlab="Number of Gears")
    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=c(0, 2.5), #Limit of line
    col="red",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks

    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=3+c(0, 2.5), #Limit of line
    col="blue",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks


    plt2






    share|improve this answer















    An example (doesn't contain GGPLOT2):



    data("mtcars")
    counts <- table(mtcars$gear)
    barplot(counts, main="Car Distribution",
    xlab="Number of Gears")
    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=c(0, 5), #Limit of line
    col="red",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks


    plt



    Multiple lines, just append another axis command as -



    data("mtcars")
    counts <- table(mtcars$gear)
    barplot(counts, main="Car Distribution",
    xlab="Number of Gears")
    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=c(0, 2.5), #Limit of line
    col="red",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks

    axis(1, # Put 1 for X-axis, 2 for Y-axis
    at=3+c(0, 2.5), #Limit of line
    col="blue",
    line=2.5, # how much gap you need between line and X-axis
    labels=rep("",2), # remove line labels
    lwd=2,
    lwd.ticks=0) # remove ticks


    plt2







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 2 at 11:19

























    answered Jan 2 at 10:27









    meWmeW

    2,745119




    2,745119













    • I get this error: Error in axis(1, at = c(0, 5), col = "red", line = 2.5, labels = rep("", : plot.new has not been called yet

      – M.R.Wani
      Jan 2 at 10:40











    • you need to call the plot command, which means plot the figure first then draw the line

      – meW
      Jan 2 at 10:41











    • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

      – M.R.Wani
      Jan 2 at 10:44













    • What do you mean by these variables? Elaborate.

      – meW
      Jan 2 at 10:45











    • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

      – M.R.Wani
      Jan 2 at 10:51



















    • I get this error: Error in axis(1, at = c(0, 5), col = "red", line = 2.5, labels = rep("", : plot.new has not been called yet

      – M.R.Wani
      Jan 2 at 10:40











    • you need to call the plot command, which means plot the figure first then draw the line

      – meW
      Jan 2 at 10:41











    • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

      – M.R.Wani
      Jan 2 at 10:44













    • What do you mean by these variables? Elaborate.

      – meW
      Jan 2 at 10:45











    • If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

      – M.R.Wani
      Jan 2 at 10:51

















    I get this error: Error in axis(1, at = c(0, 5), col = "red", line = 2.5, labels = rep("", : plot.new has not been called yet

    – M.R.Wani
    Jan 2 at 10:40





    I get this error: Error in axis(1, at = c(0, 5), col = "red", line = 2.5, labels = rep("", : plot.new has not been called yet

    – M.R.Wani
    Jan 2 at 10:40













    you need to call the plot command, which means plot the figure first then draw the line

    – meW
    Jan 2 at 10:41





    you need to call the plot command, which means plot the figure first then draw the line

    – meW
    Jan 2 at 10:41













    If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

    – M.R.Wani
    Jan 2 at 10:44







    If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

    – M.R.Wani
    Jan 2 at 10:44















    What do you mean by these variables? Elaborate.

    – meW
    Jan 2 at 10:45





    What do you mean by these variables? Elaborate.

    – meW
    Jan 2 at 10:45













    If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

    – M.R.Wani
    Jan 2 at 10:51





    If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE )

    – M.R.Wani
    Jan 2 at 10:51













    0














    Maybe the facet_wrap from ggplot2 is a starting point:





    library(magrittr)
    library(ggplot2)

    mtcars %>%
    dplyr::mutate(hp_200 = ifelse(hp > 200, "hp > 200", "hp <= 200")) %>%
    ggplot(aes(x = hp)) +
    geom_histogram(binwidth = 20) +
    facet_wrap(~ hp_200, scales = "free_x", strip.position = "bottom") +
    ggthemes::theme_hc()




    Created on 2019-01-02 by the reprex package (v0.2.1)






    share|improve this answer




























      0














      Maybe the facet_wrap from ggplot2 is a starting point:





      library(magrittr)
      library(ggplot2)

      mtcars %>%
      dplyr::mutate(hp_200 = ifelse(hp > 200, "hp > 200", "hp <= 200")) %>%
      ggplot(aes(x = hp)) +
      geom_histogram(binwidth = 20) +
      facet_wrap(~ hp_200, scales = "free_x", strip.position = "bottom") +
      ggthemes::theme_hc()




      Created on 2019-01-02 by the reprex package (v0.2.1)






      share|improve this answer


























        0












        0








        0







        Maybe the facet_wrap from ggplot2 is a starting point:





        library(magrittr)
        library(ggplot2)

        mtcars %>%
        dplyr::mutate(hp_200 = ifelse(hp > 200, "hp > 200", "hp <= 200")) %>%
        ggplot(aes(x = hp)) +
        geom_histogram(binwidth = 20) +
        facet_wrap(~ hp_200, scales = "free_x", strip.position = "bottom") +
        ggthemes::theme_hc()




        Created on 2019-01-02 by the reprex package (v0.2.1)






        share|improve this answer













        Maybe the facet_wrap from ggplot2 is a starting point:





        library(magrittr)
        library(ggplot2)

        mtcars %>%
        dplyr::mutate(hp_200 = ifelse(hp > 200, "hp > 200", "hp <= 200")) %>%
        ggplot(aes(x = hp)) +
        geom_histogram(binwidth = 20) +
        facet_wrap(~ hp_200, scales = "free_x", strip.position = "bottom") +
        ggthemes::theme_hc()




        Created on 2019-01-02 by the reprex package (v0.2.1)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 17:10









        BirgerBirger

        655112




        655112






























            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%2f54004510%2fhow-can-i-add-a-line-below-axis-labels-in-ggplot2%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas