R Overlay Line Plot on BarPlot with 2 axesggplot2 overlay of barplot and line plotPlot two graphs in same plot in RHow create a overlay plot with secondary y-axis?ggplot: line plot for discrete x-axisHow to superimpose a line graph on a barplot in ggplot2stacked barplot converting a variable into a presence absence based percentage for unrelated variables in ggplot2 ROverlaying barplot with line graphs using ggplot2Overlay histogram with kernel density for different data sets on different x-scaleError- State_count() must not be used ,. How to solve this?Removing axis labelling for one geom when multiple geoms are presentError: Don't know how to add e2 to a plot

declaring a variable twice in IIFE

Why is this code 6.5x slower with optimizations enabled?

Download, install and reboot computer at night if needed

What typically incentivizes a professor to change jobs to a lower ranking university?

I probably found a bug with the sudo apt install function

Is there really no realistic way for a skeleton monster to move around without magic?

The magic money tree problem

The use of multiple foreign keys on same column in SQL Server

How to calculate implied correlation via observed market price (Margrabe option)

Can a German sentence have two subjects?

Extreme, but not acceptable situation and I can't start the work tomorrow morning

Schwarzchild Radius of the Universe

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

What do you call a Matrix-like slowdown and camera movement effect?

Why CLRS example on residual networks does not follows its formula?

Why has Russell's definition of numbers using equivalence classes been finally abandoned? ( If it has actually been abandoned).

How can bays and straits be determined in a procedurally generated map?

Can I make popcorn with any corn?

What is the meaning of "of trouble" in the following sentence?

Is it possible to do 50 km distance without any previous training?

How old can references or sources in a thesis be?

What is the command to reset a PC without deleting any files

How is it possible for user's password to be changed after storage was encrypted? (on OS X, Android)

Motorized valve interfering with button?



R Overlay Line Plot on BarPlot with 2 axes


ggplot2 overlay of barplot and line plotPlot two graphs in same plot in RHow create a overlay plot with secondary y-axis?ggplot: line plot for discrete x-axisHow to superimpose a line graph on a barplot in ggplot2stacked barplot converting a variable into a presence absence based percentage for unrelated variables in ggplot2 ROverlaying barplot with line graphs using ggplot2Overlay histogram with kernel density for different data sets on different x-scaleError- State_count() must not be used ,. How to solve this?Removing axis labelling for one geom when multiple geoms are presentError: Don't know how to add e2 to a plot






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I have the following code and it is only producing a barplot without the line. How can I tweak this code to get the secondary line?
enter image description here






library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)





I've looked at ggplot2 overlay of barplot and line plot and I still can't figure out why the plot isn't showing the line.



enter image description here










share|improve this question






























    2















    I have the following code and it is only producing a barplot without the line. How can I tweak this code to get the secondary line?
    enter image description here






    library(ggplot2)
    p1 <- ggplot()
    p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
    p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
    p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
    p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
    print(p1)





    I've looked at ggplot2 overlay of barplot and line plot and I still can't figure out why the plot isn't showing the line.



    enter image description here










    share|improve this question


























      2












      2








      2








      I have the following code and it is only producing a barplot without the line. How can I tweak this code to get the secondary line?
      enter image description here






      library(ggplot2)
      p1 <- ggplot()
      p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
      p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
      p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
      p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
      print(p1)





      I've looked at ggplot2 overlay of barplot and line plot and I still can't figure out why the plot isn't showing the line.



      enter image description here










      share|improve this question
















      I have the following code and it is only producing a barplot without the line. How can I tweak this code to get the secondary line?
      enter image description here






      library(ggplot2)
      p1 <- ggplot()
      p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
      p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
      p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
      p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
      print(p1)





      I've looked at ggplot2 overlay of barplot and line plot and I still can't figure out why the plot isn't showing the line.



      enter image description here






      library(ggplot2)
      p1 <- ggplot()
      p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
      p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
      p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
      p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
      print(p1)





      library(ggplot2)
      p1 <- ggplot()
      p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
      p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
      p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
      p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
      print(p1)






      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 4:43







      bross

















      asked Mar 8 at 6:02









      brossbross

      385




      385






















          2 Answers
          2






          active

          oldest

          votes


















          0














          You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:



          p1 <- p1 + geom_line(data = subset(df, Year == 2006), 
          aes(x = State, y = Total.Poverty / 1000), colour = "blue")


          instead.






          share|improve this answer























          • I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]

            – bross
            Mar 9 at 4:40



















          0














          Had to add group = 1 in the code






          library(ggplot2)
          p1 <- ggplot()
          p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
          p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
          p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
          p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
          print(p1)








          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%2f55057566%2fr-overlay-line-plot-on-barplot-with-2-axes%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









            0














            You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:



            p1 <- p1 + geom_line(data = subset(df, Year == 2006), 
            aes(x = State, y = Total.Poverty / 1000), colour = "blue")


            instead.






            share|improve this answer























            • I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]

              – bross
              Mar 9 at 4:40
















            0














            You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:



            p1 <- p1 + geom_line(data = subset(df, Year == 2006), 
            aes(x = State, y = Total.Poverty / 1000), colour = "blue")


            instead.






            share|improve this answer























            • I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]

              – bross
              Mar 9 at 4:40














            0












            0








            0







            You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:



            p1 <- p1 + geom_line(data = subset(df, Year == 2006), 
            aes(x = State, y = Total.Poverty / 1000), colour = "blue")


            instead.






            share|improve this answer













            You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:



            p1 <- p1 + geom_line(data = subset(df, Year == 2006), 
            aes(x = State, y = Total.Poverty / 1000), colour = "blue")


            instead.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 8 at 12:48









            thothalthothal

            4,8351337




            4,8351337












            • I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]

              – bross
              Mar 9 at 4:40


















            • I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]

              – bross
              Mar 9 at 4:40

















            I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]

            – bross
            Mar 9 at 4:40






            I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]

            – bross
            Mar 9 at 4:40














            0














            Had to add group = 1 in the code






            library(ggplot2)
            p1 <- ggplot()
            p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
            p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
            p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
            p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
            print(p1)








            share|improve this answer



























              0














              Had to add group = 1 in the code






              library(ggplot2)
              p1 <- ggplot()
              p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
              p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
              p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
              p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
              print(p1)








              share|improve this answer

























                0












                0








                0







                Had to add group = 1 in the code






                library(ggplot2)
                p1 <- ggplot()
                p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
                p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
                p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
                p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
                print(p1)








                share|improve this answer













                Had to add group = 1 in the code






                library(ggplot2)
                p1 <- ggplot()
                p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
                p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
                p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
                p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
                print(p1)








                library(ggplot2)
                p1 <- ggplot()
                p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
                p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
                p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
                p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
                print(p1)





                library(ggplot2)
                p1 <- ggplot()
                p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
                p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
                p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
                p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
                print(p1)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 9 at 4:50









                brossbross

                385




                385



























                    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%2f55057566%2fr-overlay-line-plot-on-barplot-with-2-axes%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

                    Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                    Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                    Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved