Adding point to a facetHow to add different lines for facetsAdd a segment only to one facet using ggplot2Month-Year bar graph plot faceted and filled on year; with data input as date in char formatAdding R^2 on graph with facetsScaling data in R data frame and fitting gaussian to geom_pointggplot2: facet_wrap strip color based on variable in data setgeom_vlines multiple vlines per plotMapping shape number to legendHow to use the facet grid or facet wrap method in ggplot2 with stacked bar plot?leveled or factored variable used in facets and incorrect geom_text labelingChanging Chart colour scale in loopColor facets in ggplot by continuous variable

A case of the sniffles

"You are your self first supporter", a more proper way to say it

Can a Cauchy sequence converge for one metric while not converging for another?

How does one intimidate enemies without having the capacity for violence?

How old can references or sources in a thesis be?

How does quantile regression compare to logistic regression with the variable split at the quantile?

How to format long polynomial?

I'm flying to France today and my passport expires in less than 2 months

Modeling an IP Address

infared filters v nd

Was any UN Security Council vote triple-vetoed?

What's that red-plus icon near a text?

Revoked SSL certificate

Why can't I see bouncing of a switch on an oscilloscope?

Malformed Address '10.10.21.08/24', must be X.X.X.X/NN or

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

High voltage LED indicator 40-1000 VDC without additional power supply

Why is consensus so controversial in Britain?

Today is the Center

How much of data wrangling is a data scientist's job?

Add text to same line using sed

What's the output of a record needle playing an out-of-speed record

Does an object always see its latest internal state irrespective of thread?

Is it inappropriate for a student to attend their mentor's dissertation defense?



Adding point to a facet


How to add different lines for facetsAdd a segment only to one facet using ggplot2Month-Year bar graph plot faceted and filled on year; with data input as date in char formatAdding R^2 on graph with facetsScaling data in R data frame and fitting gaussian to geom_pointggplot2: facet_wrap strip color based on variable in data setgeom_vlines multiple vlines per plotMapping shape number to legendHow to use the facet grid or facet wrap method in ggplot2 with stacked bar plot?leveled or factored variable used in facets and incorrect geom_text labelingChanging Chart colour scale in loopColor facets in ggplot by continuous variable






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








0















First, the libraries



library(tidyr)
library(leaps)
library(ggplots2)
library(ggdark)


The value of the model



set.seed(1)
X = rnorm(100)
e = rnorm(100)
Y = 8 + 7*X + 2.5*X^2 - 9*X^3 + e


Fitting



data.all = data.frame(Y,X)
regfit.full = regsubsets(Y~poly(X,10,raw=T), data=data.all, nvmax=10)
(reg.summary = summary(regfit.full))


Then I get the minimum value for each variables



(reg.min.cp = which.min(reg.summary$cp))
(reg.min.bic = which.min(reg.summary$bic))
(reg.min.adjr2 = which.min(reg.summary$adjr2))


Creating the data frame for plot



df = data.frame(reg.summary$cp, reg.summary$bic, reg.summary$adjr2)
df$rownum = 1:nrow(df)


Reshaping the data frame



molten = df %>% gather(variable, value, reg.summary.cp:reg.summary.adjr2 )


Plotting with facets



(lp = molten %>% ggplot(data=.) + 
aes(x=rownum, y=value) +
geom_line(col="black") +
geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) + # this is where I got the wrong plot
facet_wrap(~variable, scales="free_y")
)


enter image description here



And it shows wrong. What I expect is that the geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) will just add the reg.min.adjr2 to the facet reg.summary.adjr2 and only one point.
How to make it in that way?










share|improve this question




























    0















    First, the libraries



    library(tidyr)
    library(leaps)
    library(ggplots2)
    library(ggdark)


    The value of the model



    set.seed(1)
    X = rnorm(100)
    e = rnorm(100)
    Y = 8 + 7*X + 2.5*X^2 - 9*X^3 + e


    Fitting



    data.all = data.frame(Y,X)
    regfit.full = regsubsets(Y~poly(X,10,raw=T), data=data.all, nvmax=10)
    (reg.summary = summary(regfit.full))


    Then I get the minimum value for each variables



    (reg.min.cp = which.min(reg.summary$cp))
    (reg.min.bic = which.min(reg.summary$bic))
    (reg.min.adjr2 = which.min(reg.summary$adjr2))


    Creating the data frame for plot



    df = data.frame(reg.summary$cp, reg.summary$bic, reg.summary$adjr2)
    df$rownum = 1:nrow(df)


    Reshaping the data frame



    molten = df %>% gather(variable, value, reg.summary.cp:reg.summary.adjr2 )


    Plotting with facets



    (lp = molten %>% ggplot(data=.) + 
    aes(x=rownum, y=value) +
    geom_line(col="black") +
    geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) + # this is where I got the wrong plot
    facet_wrap(~variable, scales="free_y")
    )


    enter image description here



    And it shows wrong. What I expect is that the geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) will just add the reg.min.adjr2 to the facet reg.summary.adjr2 and only one point.
    How to make it in that way?










    share|improve this question
























      0












      0








      0








      First, the libraries



      library(tidyr)
      library(leaps)
      library(ggplots2)
      library(ggdark)


      The value of the model



      set.seed(1)
      X = rnorm(100)
      e = rnorm(100)
      Y = 8 + 7*X + 2.5*X^2 - 9*X^3 + e


      Fitting



      data.all = data.frame(Y,X)
      regfit.full = regsubsets(Y~poly(X,10,raw=T), data=data.all, nvmax=10)
      (reg.summary = summary(regfit.full))


      Then I get the minimum value for each variables



      (reg.min.cp = which.min(reg.summary$cp))
      (reg.min.bic = which.min(reg.summary$bic))
      (reg.min.adjr2 = which.min(reg.summary$adjr2))


      Creating the data frame for plot



      df = data.frame(reg.summary$cp, reg.summary$bic, reg.summary$adjr2)
      df$rownum = 1:nrow(df)


      Reshaping the data frame



      molten = df %>% gather(variable, value, reg.summary.cp:reg.summary.adjr2 )


      Plotting with facets



      (lp = molten %>% ggplot(data=.) + 
      aes(x=rownum, y=value) +
      geom_line(col="black") +
      geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) + # this is where I got the wrong plot
      facet_wrap(~variable, scales="free_y")
      )


      enter image description here



      And it shows wrong. What I expect is that the geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) will just add the reg.min.adjr2 to the facet reg.summary.adjr2 and only one point.
      How to make it in that way?










      share|improve this question














      First, the libraries



      library(tidyr)
      library(leaps)
      library(ggplots2)
      library(ggdark)


      The value of the model



      set.seed(1)
      X = rnorm(100)
      e = rnorm(100)
      Y = 8 + 7*X + 2.5*X^2 - 9*X^3 + e


      Fitting



      data.all = data.frame(Y,X)
      regfit.full = regsubsets(Y~poly(X,10,raw=T), data=data.all, nvmax=10)
      (reg.summary = summary(regfit.full))


      Then I get the minimum value for each variables



      (reg.min.cp = which.min(reg.summary$cp))
      (reg.min.bic = which.min(reg.summary$bic))
      (reg.min.adjr2 = which.min(reg.summary$adjr2))


      Creating the data frame for plot



      df = data.frame(reg.summary$cp, reg.summary$bic, reg.summary$adjr2)
      df$rownum = 1:nrow(df)


      Reshaping the data frame



      molten = df %>% gather(variable, value, reg.summary.cp:reg.summary.adjr2 )


      Plotting with facets



      (lp = molten %>% ggplot(data=.) + 
      aes(x=rownum, y=value) +
      geom_line(col="black") +
      geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) + # this is where I got the wrong plot
      facet_wrap(~variable, scales="free_y")
      )


      enter image description here



      And it shows wrong. What I expect is that the geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) will just add the reg.min.adjr2 to the facet reg.summary.adjr2 and only one point.
      How to make it in that way?







      r ggplot2 facet






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 1:56









      isemajisemaj

      408




      408






















          1 Answer
          1






          active

          oldest

          votes


















          0














          I got some idea here from these two SO:



          How to add different lines for facets



          Add a segment only to one facet using ggplot2



          What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.



          I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.



          min_plot = data.frame(
          rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
          y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
          variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))

          (lp = molten %>% ggplot(data=.)
          + aes(x=rownum, y=value)
          + geom_line(col="black")
          + facet_wrap(~variable, scales="free_y")
          + geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
          )





          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%2f55055659%2fadding-point-to-a-facet%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I got some idea here from these two SO:



            How to add different lines for facets



            Add a segment only to one facet using ggplot2



            What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.



            I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.



            min_plot = data.frame(
            rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
            y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
            variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))

            (lp = molten %>% ggplot(data=.)
            + aes(x=rownum, y=value)
            + geom_line(col="black")
            + facet_wrap(~variable, scales="free_y")
            + geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
            )





            share|improve this answer



























              0














              I got some idea here from these two SO:



              How to add different lines for facets



              Add a segment only to one facet using ggplot2



              What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.



              I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.



              min_plot = data.frame(
              rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
              y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
              variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))

              (lp = molten %>% ggplot(data=.)
              + aes(x=rownum, y=value)
              + geom_line(col="black")
              + facet_wrap(~variable, scales="free_y")
              + geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
              )





              share|improve this answer

























                0












                0








                0







                I got some idea here from these two SO:



                How to add different lines for facets



                Add a segment only to one facet using ggplot2



                What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.



                I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.



                min_plot = data.frame(
                rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
                y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
                variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))

                (lp = molten %>% ggplot(data=.)
                + aes(x=rownum, y=value)
                + geom_line(col="black")
                + facet_wrap(~variable, scales="free_y")
                + geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
                )





                share|improve this answer













                I got some idea here from these two SO:



                How to add different lines for facets



                Add a segment only to one facet using ggplot2



                What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.



                I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.



                min_plot = data.frame(
                rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
                y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
                variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))

                (lp = molten %>% ggplot(data=.)
                + aes(x=rownum, y=value)
                + geom_line(col="black")
                + facet_wrap(~variable, scales="free_y")
                + geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
                )






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 8 at 2:35









                isemajisemaj

                408




                408





























                    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%2f55055659%2fadding-point-to-a-facet%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