R Overlay Line Plot on BarPlot with 2 axes Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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

In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω

TV series episode where humans nuke aliens before decrypting their message that states they come in peace

Married in secret, can marital status in passport be changed at a later date?

How would you suggest I follow up with coworkers about our deadline that's today?

Is it appropriate to mention a relatable company blog post when you're asked about the company?

What helicopter has the most rotor blades?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

When does Bran Stark remember Jamie pushing him?

Is Bran literally the world's memory?

Why do people think Winterfell crypts is the safest place for women, children & old people?

Preserving file and folder permissions with rsync

Is there an efficient way for synchronising audio events real-time with LEDs using an MCU?

Suing a Police Officer Instead of the Police Department

Philosophers who were composers?

What is the numbering system used for the DSN dishes?

What do you call an IPA symbol that lacks a name (e.g. ɲ)?

Coin Game with infinite paradox

`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

Are there existing rules/lore for MTG planeswalkers?

Processing ADC conversion result: DMA vs Processor Registers

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

Is there a way to fake a method response using Mock or Stubs?

Determinant of a matrix with 2 equal rows



R Overlay Line Plot on BarPlot with 2 axes



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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

            5,5491438




            5,5491438












            • 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