How to change y axis limits in decimal points in R?












1














I can plot the barplot in Excel with decimal points in y-axis limits as shown below.



Graph1



But I would not be able to change the y-axis limits in R.



Graph2



Here is my code in R.



par(mfrow=c(1, 1), mar=c(7, 4, 5, 6))
mydata <- data.frame(Algorithm1=c(95.85, 96.94), Algorithm2=c(96.04, 96.84), Algorithm3=c(95, 95.30))
barplot(as.matrix(mydata), main="Precision", ylim=range(0:100),
beside=T, col=c("red", "blue"), las=1, border = 0, cex.lab=1, cex.axis=1, font=1,col.axis="black", ylab = "Percentage",
legend.text = c("X1", "X2"),
args.legend = list(x ='topright', bty='n', inset=c(-0.20,0)))


Thanks in advance for your answer.










share|improve this question



























    1














    I can plot the barplot in Excel with decimal points in y-axis limits as shown below.



    Graph1



    But I would not be able to change the y-axis limits in R.



    Graph2



    Here is my code in R.



    par(mfrow=c(1, 1), mar=c(7, 4, 5, 6))
    mydata <- data.frame(Algorithm1=c(95.85, 96.94), Algorithm2=c(96.04, 96.84), Algorithm3=c(95, 95.30))
    barplot(as.matrix(mydata), main="Precision", ylim=range(0:100),
    beside=T, col=c("red", "blue"), las=1, border = 0, cex.lab=1, cex.axis=1, font=1,col.axis="black", ylab = "Percentage",
    legend.text = c("X1", "X2"),
    args.legend = list(x ='topright', bty='n', inset=c(-0.20,0)))


    Thanks in advance for your answer.










    share|improve this question

























      1












      1








      1







      I can plot the barplot in Excel with decimal points in y-axis limits as shown below.



      Graph1



      But I would not be able to change the y-axis limits in R.



      Graph2



      Here is my code in R.



      par(mfrow=c(1, 1), mar=c(7, 4, 5, 6))
      mydata <- data.frame(Algorithm1=c(95.85, 96.94), Algorithm2=c(96.04, 96.84), Algorithm3=c(95, 95.30))
      barplot(as.matrix(mydata), main="Precision", ylim=range(0:100),
      beside=T, col=c("red", "blue"), las=1, border = 0, cex.lab=1, cex.axis=1, font=1,col.axis="black", ylab = "Percentage",
      legend.text = c("X1", "X2"),
      args.legend = list(x ='topright', bty='n', inset=c(-0.20,0)))


      Thanks in advance for your answer.










      share|improve this question













      I can plot the barplot in Excel with decimal points in y-axis limits as shown below.



      Graph1



      But I would not be able to change the y-axis limits in R.



      Graph2



      Here is my code in R.



      par(mfrow=c(1, 1), mar=c(7, 4, 5, 6))
      mydata <- data.frame(Algorithm1=c(95.85, 96.94), Algorithm2=c(96.04, 96.84), Algorithm3=c(95, 95.30))
      barplot(as.matrix(mydata), main="Precision", ylim=range(0:100),
      beside=T, col=c("red", "blue"), las=1, border = 0, cex.lab=1, cex.axis=1, font=1,col.axis="black", ylab = "Percentage",
      legend.text = c("X1", "X2"),
      args.legend = list(x ='topright', bty='n', inset=c(-0.20,0)))


      Thanks in advance for your answer.







      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 27 at 14:32









      Ashok Kumar Jayaraman

      991715




      991715
























          3 Answers
          3






          active

          oldest

          votes


















          1














          Set the limit of y and xpd = FALSE.





          • FALSE : all plotting is clipped to the plot region


          • TRUE : all plotting is clipped to the figure region


          • NA : all plotting is clipped to the device region




          library(RColorBrewer)
          color <- brewer.pal(3, "Set1")[2:1]
          plot.new()
          plot.window(xlim = c(0, 10), ylim = c(94, 97.5), yaxs = "i")
          abline(h = seq(94, 97.5, 0.5), col = 8)
          barplot(as.matrix(mydata), beside = T, col = color,
          border = NA, legend.text = c("X1", "X2"),
          args.legend = list(x = 'topright', bty = "n"), xpd = F, add = T)


          enter image description here






          share|improve this answer































            4














            You can also use ggplot2 and scales.



            library(dplyr)
            library(ggplot2)
            library(scales)
            mydata <- data.frame(Algorithm = rep(c('Algorithm1','Algorithm2','Algorithm3'), each=2),
            variable_x = rep(c('X1','X2'),3),
            values=c(0.9585, 0.9694,0.9604, 0.9684, 0.95, 0.9530))

            mydata %>%
            ggplot(aes(x=Algorithm,y=values,fill=variable_x))+
            geom_bar(stat='identity', position='dodge')+
            scale_y_continuous(labels = scales::percent, limits = c(0.94,0.975), oob = rescale_none)+
            scale_fill_manual(values= c(X1='red',X2='blue'))





            share|improve this answer










            New contributor




            Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


























              0














              You could do:



              tickPoints <- 20 * (0:5)

              par(mfrow = c(1, 1), mar = c(7, 4, 5, 6))
              mydata <- data.frame(
              Algorithm1 = c(95.85, 96.94),
              Algorithm2 = c(96.04, 96.84),
              Algorithm3 = c(95, 95.30)
              )

              barplot(
              as.matrix(mydata), main = "Precision", beside = T, col = c("red", "blue"),
              las = 1, border = 0, cex.lab = 1, cex.axis = 1, font = 1, col.axis = "black",
              ylab = "Percentage", legend.text = c("X1", "X2"),
              args.legend = list(x = 'topright', bty = 'n',inset = c(-0.20, 0)),
              axes = FALSE,
              ylim = range(tickPoints)
              )

              axis(side = 2, at = tickPoints, labels = sprintf("%0.1f", tickPoints))


              Note the axes = FALSE in the call to barplot






              share|improve this answer























              • Thanks for your answer. But I want the y-axis limits as shown in the first figure.
                – Ashok Kumar Jayaraman
                Dec 28 at 3:54






              • 1




                You can update the the tickPoints to whatever suits you, for example tickPoints <- seq(94, 97.5, by = 0.5) to replicate the Excel chart
                – Jozef
                Dec 28 at 7:21











              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%2f53946680%2fhow-to-change-y-axis-limits-in-decimal-points-in-r%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Set the limit of y and xpd = FALSE.





              • FALSE : all plotting is clipped to the plot region


              • TRUE : all plotting is clipped to the figure region


              • NA : all plotting is clipped to the device region




              library(RColorBrewer)
              color <- brewer.pal(3, "Set1")[2:1]
              plot.new()
              plot.window(xlim = c(0, 10), ylim = c(94, 97.5), yaxs = "i")
              abline(h = seq(94, 97.5, 0.5), col = 8)
              barplot(as.matrix(mydata), beside = T, col = color,
              border = NA, legend.text = c("X1", "X2"),
              args.legend = list(x = 'topright', bty = "n"), xpd = F, add = T)


              enter image description here






              share|improve this answer




























                1














                Set the limit of y and xpd = FALSE.





                • FALSE : all plotting is clipped to the plot region


                • TRUE : all plotting is clipped to the figure region


                • NA : all plotting is clipped to the device region




                library(RColorBrewer)
                color <- brewer.pal(3, "Set1")[2:1]
                plot.new()
                plot.window(xlim = c(0, 10), ylim = c(94, 97.5), yaxs = "i")
                abline(h = seq(94, 97.5, 0.5), col = 8)
                barplot(as.matrix(mydata), beside = T, col = color,
                border = NA, legend.text = c("X1", "X2"),
                args.legend = list(x = 'topright', bty = "n"), xpd = F, add = T)


                enter image description here






                share|improve this answer


























                  1












                  1








                  1






                  Set the limit of y and xpd = FALSE.





                  • FALSE : all plotting is clipped to the plot region


                  • TRUE : all plotting is clipped to the figure region


                  • NA : all plotting is clipped to the device region




                  library(RColorBrewer)
                  color <- brewer.pal(3, "Set1")[2:1]
                  plot.new()
                  plot.window(xlim = c(0, 10), ylim = c(94, 97.5), yaxs = "i")
                  abline(h = seq(94, 97.5, 0.5), col = 8)
                  barplot(as.matrix(mydata), beside = T, col = color,
                  border = NA, legend.text = c("X1", "X2"),
                  args.legend = list(x = 'topright', bty = "n"), xpd = F, add = T)


                  enter image description here






                  share|improve this answer














                  Set the limit of y and xpd = FALSE.





                  • FALSE : all plotting is clipped to the plot region


                  • TRUE : all plotting is clipped to the figure region


                  • NA : all plotting is clipped to the device region




                  library(RColorBrewer)
                  color <- brewer.pal(3, "Set1")[2:1]
                  plot.new()
                  plot.window(xlim = c(0, 10), ylim = c(94, 97.5), yaxs = "i")
                  abline(h = seq(94, 97.5, 0.5), col = 8)
                  barplot(as.matrix(mydata), beside = T, col = color,
                  border = NA, legend.text = c("X1", "X2"),
                  args.legend = list(x = 'topright', bty = "n"), xpd = F, add = T)


                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 27 at 16:57

























                  answered Dec 27 at 15:54









                  Darren Tsai

                  1,109320




                  1,109320

























                      4














                      You can also use ggplot2 and scales.



                      library(dplyr)
                      library(ggplot2)
                      library(scales)
                      mydata <- data.frame(Algorithm = rep(c('Algorithm1','Algorithm2','Algorithm3'), each=2),
                      variable_x = rep(c('X1','X2'),3),
                      values=c(0.9585, 0.9694,0.9604, 0.9684, 0.95, 0.9530))

                      mydata %>%
                      ggplot(aes(x=Algorithm,y=values,fill=variable_x))+
                      geom_bar(stat='identity', position='dodge')+
                      scale_y_continuous(labels = scales::percent, limits = c(0.94,0.975), oob = rescale_none)+
                      scale_fill_manual(values= c(X1='red',X2='blue'))





                      share|improve this answer










                      New contributor




                      Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.























                        4














                        You can also use ggplot2 and scales.



                        library(dplyr)
                        library(ggplot2)
                        library(scales)
                        mydata <- data.frame(Algorithm = rep(c('Algorithm1','Algorithm2','Algorithm3'), each=2),
                        variable_x = rep(c('X1','X2'),3),
                        values=c(0.9585, 0.9694,0.9604, 0.9684, 0.95, 0.9530))

                        mydata %>%
                        ggplot(aes(x=Algorithm,y=values,fill=variable_x))+
                        geom_bar(stat='identity', position='dodge')+
                        scale_y_continuous(labels = scales::percent, limits = c(0.94,0.975), oob = rescale_none)+
                        scale_fill_manual(values= c(X1='red',X2='blue'))





                        share|improve this answer










                        New contributor




                        Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.





















                          4












                          4








                          4






                          You can also use ggplot2 and scales.



                          library(dplyr)
                          library(ggplot2)
                          library(scales)
                          mydata <- data.frame(Algorithm = rep(c('Algorithm1','Algorithm2','Algorithm3'), each=2),
                          variable_x = rep(c('X1','X2'),3),
                          values=c(0.9585, 0.9694,0.9604, 0.9684, 0.95, 0.9530))

                          mydata %>%
                          ggplot(aes(x=Algorithm,y=values,fill=variable_x))+
                          geom_bar(stat='identity', position='dodge')+
                          scale_y_continuous(labels = scales::percent, limits = c(0.94,0.975), oob = rescale_none)+
                          scale_fill_manual(values= c(X1='red',X2='blue'))





                          share|improve this answer










                          New contributor




                          Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          You can also use ggplot2 and scales.



                          library(dplyr)
                          library(ggplot2)
                          library(scales)
                          mydata <- data.frame(Algorithm = rep(c('Algorithm1','Algorithm2','Algorithm3'), each=2),
                          variable_x = rep(c('X1','X2'),3),
                          values=c(0.9585, 0.9694,0.9604, 0.9684, 0.95, 0.9530))

                          mydata %>%
                          ggplot(aes(x=Algorithm,y=values,fill=variable_x))+
                          geom_bar(stat='identity', position='dodge')+
                          scale_y_continuous(labels = scales::percent, limits = c(0.94,0.975), oob = rescale_none)+
                          scale_fill_manual(values= c(X1='red',X2='blue'))






                          share|improve this answer










                          New contributor




                          Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer








                          edited Dec 28 at 5:07









                          Darren Tsai

                          1,109320




                          1,109320






                          New contributor




                          Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered Dec 27 at 15:54









                          Senzeybek

                          414




                          414




                          New contributor




                          Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Senzeybek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.























                              0














                              You could do:



                              tickPoints <- 20 * (0:5)

                              par(mfrow = c(1, 1), mar = c(7, 4, 5, 6))
                              mydata <- data.frame(
                              Algorithm1 = c(95.85, 96.94),
                              Algorithm2 = c(96.04, 96.84),
                              Algorithm3 = c(95, 95.30)
                              )

                              barplot(
                              as.matrix(mydata), main = "Precision", beside = T, col = c("red", "blue"),
                              las = 1, border = 0, cex.lab = 1, cex.axis = 1, font = 1, col.axis = "black",
                              ylab = "Percentage", legend.text = c("X1", "X2"),
                              args.legend = list(x = 'topright', bty = 'n',inset = c(-0.20, 0)),
                              axes = FALSE,
                              ylim = range(tickPoints)
                              )

                              axis(side = 2, at = tickPoints, labels = sprintf("%0.1f", tickPoints))


                              Note the axes = FALSE in the call to barplot






                              share|improve this answer























                              • Thanks for your answer. But I want the y-axis limits as shown in the first figure.
                                – Ashok Kumar Jayaraman
                                Dec 28 at 3:54






                              • 1




                                You can update the the tickPoints to whatever suits you, for example tickPoints <- seq(94, 97.5, by = 0.5) to replicate the Excel chart
                                – Jozef
                                Dec 28 at 7:21
















                              0














                              You could do:



                              tickPoints <- 20 * (0:5)

                              par(mfrow = c(1, 1), mar = c(7, 4, 5, 6))
                              mydata <- data.frame(
                              Algorithm1 = c(95.85, 96.94),
                              Algorithm2 = c(96.04, 96.84),
                              Algorithm3 = c(95, 95.30)
                              )

                              barplot(
                              as.matrix(mydata), main = "Precision", beside = T, col = c("red", "blue"),
                              las = 1, border = 0, cex.lab = 1, cex.axis = 1, font = 1, col.axis = "black",
                              ylab = "Percentage", legend.text = c("X1", "X2"),
                              args.legend = list(x = 'topright', bty = 'n',inset = c(-0.20, 0)),
                              axes = FALSE,
                              ylim = range(tickPoints)
                              )

                              axis(side = 2, at = tickPoints, labels = sprintf("%0.1f", tickPoints))


                              Note the axes = FALSE in the call to barplot






                              share|improve this answer























                              • Thanks for your answer. But I want the y-axis limits as shown in the first figure.
                                – Ashok Kumar Jayaraman
                                Dec 28 at 3:54






                              • 1




                                You can update the the tickPoints to whatever suits you, for example tickPoints <- seq(94, 97.5, by = 0.5) to replicate the Excel chart
                                – Jozef
                                Dec 28 at 7:21














                              0












                              0








                              0






                              You could do:



                              tickPoints <- 20 * (0:5)

                              par(mfrow = c(1, 1), mar = c(7, 4, 5, 6))
                              mydata <- data.frame(
                              Algorithm1 = c(95.85, 96.94),
                              Algorithm2 = c(96.04, 96.84),
                              Algorithm3 = c(95, 95.30)
                              )

                              barplot(
                              as.matrix(mydata), main = "Precision", beside = T, col = c("red", "blue"),
                              las = 1, border = 0, cex.lab = 1, cex.axis = 1, font = 1, col.axis = "black",
                              ylab = "Percentage", legend.text = c("X1", "X2"),
                              args.legend = list(x = 'topright', bty = 'n',inset = c(-0.20, 0)),
                              axes = FALSE,
                              ylim = range(tickPoints)
                              )

                              axis(side = 2, at = tickPoints, labels = sprintf("%0.1f", tickPoints))


                              Note the axes = FALSE in the call to barplot






                              share|improve this answer














                              You could do:



                              tickPoints <- 20 * (0:5)

                              par(mfrow = c(1, 1), mar = c(7, 4, 5, 6))
                              mydata <- data.frame(
                              Algorithm1 = c(95.85, 96.94),
                              Algorithm2 = c(96.04, 96.84),
                              Algorithm3 = c(95, 95.30)
                              )

                              barplot(
                              as.matrix(mydata), main = "Precision", beside = T, col = c("red", "blue"),
                              las = 1, border = 0, cex.lab = 1, cex.axis = 1, font = 1, col.axis = "black",
                              ylab = "Percentage", legend.text = c("X1", "X2"),
                              args.legend = list(x = 'topright', bty = 'n',inset = c(-0.20, 0)),
                              axes = FALSE,
                              ylim = range(tickPoints)
                              )

                              axis(side = 2, at = tickPoints, labels = sprintf("%0.1f", tickPoints))


                              Note the axes = FALSE in the call to barplot







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 27 at 15:02

























                              answered Dec 27 at 14:57









                              Jozef

                              561110




                              561110












                              • Thanks for your answer. But I want the y-axis limits as shown in the first figure.
                                – Ashok Kumar Jayaraman
                                Dec 28 at 3:54






                              • 1




                                You can update the the tickPoints to whatever suits you, for example tickPoints <- seq(94, 97.5, by = 0.5) to replicate the Excel chart
                                – Jozef
                                Dec 28 at 7:21


















                              • Thanks for your answer. But I want the y-axis limits as shown in the first figure.
                                – Ashok Kumar Jayaraman
                                Dec 28 at 3:54






                              • 1




                                You can update the the tickPoints to whatever suits you, for example tickPoints <- seq(94, 97.5, by = 0.5) to replicate the Excel chart
                                – Jozef
                                Dec 28 at 7:21
















                              Thanks for your answer. But I want the y-axis limits as shown in the first figure.
                              – Ashok Kumar Jayaraman
                              Dec 28 at 3:54




                              Thanks for your answer. But I want the y-axis limits as shown in the first figure.
                              – Ashok Kumar Jayaraman
                              Dec 28 at 3:54




                              1




                              1




                              You can update the the tickPoints to whatever suits you, for example tickPoints <- seq(94, 97.5, by = 0.5) to replicate the Excel chart
                              – Jozef
                              Dec 28 at 7:21




                              You can update the the tickPoints to whatever suits you, for example tickPoints <- seq(94, 97.5, by = 0.5) to replicate the Excel chart
                              – Jozef
                              Dec 28 at 7:21


















                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53946680%2fhow-to-change-y-axis-limits-in-decimal-points-in-r%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

                              Mossoró

                              Error while reading .h5 file using the rhdf5 package in R

                              Pushsharp Apns notification error: 'InvalidToken'