Multi-line legend text including exponent with ggplot











up vote
6
down vote

favorite
2












With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:



leakage_rates_levels <- c(5.4, 0.25)
leakage_rates <- as.factor(rep(leakage_rates_levels, 3)) # L/s-m^2 at 75 Pa
data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
data_groups <- as.factor(rep(data_groups_levels, each=2))
moisture_level <- c(7, 3, 11, 10, 16, 6)
plotdt <- data.frame(data_groups, leakage_rates, moisture_level)


I use expression() to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.



ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=expression(paste('Leakage Ratenat 75 Pan(L/s-', m^2, ')', sep=''))) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())


The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.



Three line legend text where the third line indicates the units, liters per second meter squared, but the meter squared term is pushed to the right margin while all the other text is left aligned, leaving a large white space in the middle of the third line.



Using legend_title_align=0 (suggested here) and/or legend_title=element_text(hjust=1) in theme() have no effect. Trying to add phantom() spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.



Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?



I am using ggplot 3.1.0 and R 3.5.1.










share|improve this question


























    up vote
    6
    down vote

    favorite
    2












    With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:



    leakage_rates_levels <- c(5.4, 0.25)
    leakage_rates <- as.factor(rep(leakage_rates_levels, 3)) # L/s-m^2 at 75 Pa
    data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
    data_groups <- as.factor(rep(data_groups_levels, each=2))
    moisture_level <- c(7, 3, 11, 10, 16, 6)
    plotdt <- data.frame(data_groups, leakage_rates, moisture_level)


    I use expression() to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.



    ggplot(plotdt, aes(data_groups)) +
    geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
    labs(y='Moisture Level') +
    labs(fill=expression(paste('Leakage Ratenat 75 Pan(L/s-', m^2, ')', sep=''))) +
    theme(panel.grid.major.x = element_blank(),
    axis.title.x = element_blank())


    The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.



    Three line legend text where the third line indicates the units, liters per second meter squared, but the meter squared term is pushed to the right margin while all the other text is left aligned, leaving a large white space in the middle of the third line.



    Using legend_title_align=0 (suggested here) and/or legend_title=element_text(hjust=1) in theme() have no effect. Trying to add phantom() spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.



    Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?



    I am using ggplot 3.1.0 and R 3.5.1.










    share|improve this question
























      up vote
      6
      down vote

      favorite
      2









      up vote
      6
      down vote

      favorite
      2






      2





      With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:



      leakage_rates_levels <- c(5.4, 0.25)
      leakage_rates <- as.factor(rep(leakage_rates_levels, 3)) # L/s-m^2 at 75 Pa
      data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
      data_groups <- as.factor(rep(data_groups_levels, each=2))
      moisture_level <- c(7, 3, 11, 10, 16, 6)
      plotdt <- data.frame(data_groups, leakage_rates, moisture_level)


      I use expression() to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.



      ggplot(plotdt, aes(data_groups)) +
      geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
      labs(y='Moisture Level') +
      labs(fill=expression(paste('Leakage Ratenat 75 Pan(L/s-', m^2, ')', sep=''))) +
      theme(panel.grid.major.x = element_blank(),
      axis.title.x = element_blank())


      The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.



      Three line legend text where the third line indicates the units, liters per second meter squared, but the meter squared term is pushed to the right margin while all the other text is left aligned, leaving a large white space in the middle of the third line.



      Using legend_title_align=0 (suggested here) and/or legend_title=element_text(hjust=1) in theme() have no effect. Trying to add phantom() spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.



      Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?



      I am using ggplot 3.1.0 and R 3.5.1.










      share|improve this question













      With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:



      leakage_rates_levels <- c(5.4, 0.25)
      leakage_rates <- as.factor(rep(leakage_rates_levels, 3)) # L/s-m^2 at 75 Pa
      data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
      data_groups <- as.factor(rep(data_groups_levels, each=2))
      moisture_level <- c(7, 3, 11, 10, 16, 6)
      plotdt <- data.frame(data_groups, leakage_rates, moisture_level)


      I use expression() to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.



      ggplot(plotdt, aes(data_groups)) +
      geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
      labs(y='Moisture Level') +
      labs(fill=expression(paste('Leakage Ratenat 75 Pan(L/s-', m^2, ')', sep=''))) +
      theme(panel.grid.major.x = element_blank(),
      axis.title.x = element_blank())


      The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.



      Three line legend text where the third line indicates the units, liters per second meter squared, but the meter squared term is pushed to the right margin while all the other text is left aligned, leaving a large white space in the middle of the third line.



      Using legend_title_align=0 (suggested here) and/or legend_title=element_text(hjust=1) in theme() have no effect. Trying to add phantom() spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.



      Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?



      I am using ggplot 3.1.0 and R 3.5.1.







      r ggplot2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 at 19:30









      trynthink

      101210




      101210
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You can use the unicode representation of superscript two (U+00B2) and avoid the
          problem-causing combination of expression() and a multi-line legend title:



          ggplot(plotdt, aes(data_groups)) +
          geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
          labs(y='Moisture Level') +
          labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
          theme(panel.grid.major.x = element_blank(),
          axis.title.x = element_blank())


          Plot using Superscript






          share|improve this answer




























            up vote
            1
            down vote













            You can use atop to have lines "atop" each other.



            Because you have 3 lines and atop only accepts 2 arguments however, you need to have 2 atop nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle or displaystyle:



            ggplot(plotdt, aes(data_groups)) +
            geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
            labs(y = "Moisture Level") +
            labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
            textstyle("at 75 Pa")),
            "(L/s-" ~m^2~ ")"))) +
            theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())


            enter image description here






            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',
              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%2f53364672%2fmulti-line-legend-text-including-exponent-with-ggplot%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








              up vote
              4
              down vote



              accepted










              You can use the unicode representation of superscript two (U+00B2) and avoid the
              problem-causing combination of expression() and a multi-line legend title:



              ggplot(plotdt, aes(data_groups)) +
              geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
              labs(y='Moisture Level') +
              labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
              theme(panel.grid.major.x = element_blank(),
              axis.title.x = element_blank())


              Plot using Superscript






              share|improve this answer

























                up vote
                4
                down vote



                accepted










                You can use the unicode representation of superscript two (U+00B2) and avoid the
                problem-causing combination of expression() and a multi-line legend title:



                ggplot(plotdt, aes(data_groups)) +
                geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
                labs(y='Moisture Level') +
                labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
                theme(panel.grid.major.x = element_blank(),
                axis.title.x = element_blank())


                Plot using Superscript






                share|improve this answer























                  up vote
                  4
                  down vote



                  accepted







                  up vote
                  4
                  down vote



                  accepted






                  You can use the unicode representation of superscript two (U+00B2) and avoid the
                  problem-causing combination of expression() and a multi-line legend title:



                  ggplot(plotdt, aes(data_groups)) +
                  geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
                  labs(y='Moisture Level') +
                  labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
                  theme(panel.grid.major.x = element_blank(),
                  axis.title.x = element_blank())


                  Plot using Superscript






                  share|improve this answer












                  You can use the unicode representation of superscript two (U+00B2) and avoid the
                  problem-causing combination of expression() and a multi-line legend title:



                  ggplot(plotdt, aes(data_groups)) +
                  geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
                  labs(y='Moisture Level') +
                  labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
                  theme(panel.grid.major.x = element_blank(),
                  axis.title.x = element_blank())


                  Plot using Superscript







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 at 20:33









                  Mr. Zen

                  419214




                  419214
























                      up vote
                      1
                      down vote













                      You can use atop to have lines "atop" each other.



                      Because you have 3 lines and atop only accepts 2 arguments however, you need to have 2 atop nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle or displaystyle:



                      ggplot(plotdt, aes(data_groups)) +
                      geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
                      labs(y = "Moisture Level") +
                      labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
                      textstyle("at 75 Pa")),
                      "(L/s-" ~m^2~ ")"))) +
                      theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())


                      enter image description here






                      share|improve this answer



























                        up vote
                        1
                        down vote













                        You can use atop to have lines "atop" each other.



                        Because you have 3 lines and atop only accepts 2 arguments however, you need to have 2 atop nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle or displaystyle:



                        ggplot(plotdt, aes(data_groups)) +
                        geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
                        labs(y = "Moisture Level") +
                        labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
                        textstyle("at 75 Pa")),
                        "(L/s-" ~m^2~ ")"))) +
                        theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())


                        enter image description here






                        share|improve this answer

























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You can use atop to have lines "atop" each other.



                          Because you have 3 lines and atop only accepts 2 arguments however, you need to have 2 atop nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle or displaystyle:



                          ggplot(plotdt, aes(data_groups)) +
                          geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
                          labs(y = "Moisture Level") +
                          labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
                          textstyle("at 75 Pa")),
                          "(L/s-" ~m^2~ ")"))) +
                          theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())


                          enter image description here






                          share|improve this answer














                          You can use atop to have lines "atop" each other.



                          Because you have 3 lines and atop only accepts 2 arguments however, you need to have 2 atop nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle or displaystyle:



                          ggplot(plotdt, aes(data_groups)) +
                          geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
                          labs(y = "Moisture Level") +
                          labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
                          textstyle("at 75 Pa")),
                          "(L/s-" ~m^2~ ")"))) +
                          theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())


                          enter image description here







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 19 at 1:45

























                          answered Nov 18 at 21:44









                          prosoitos

                          822219




                          822219






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364672%2fmulti-line-legend-text-including-exponent-with-ggplot%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

                              Сан-Квентин

                              Алькесар

                              Josef Freinademetz