Plotting categorical variables OLS in R










2














I am trying to produce a plot with age in the x-axis, expected serum urate in the y-axis and lines for male/white, female/white, male/black, female/black, using the estimates from the lm() function.



goutdata <- read.table("gout.txt", header = TRUE)
goutdata$sex <- factor(goutdata$sex,levels = c("M", "F"))
goutdata$race <- as.factor(goutdata$race)

fm <- lm(su~sex+race+age, data = goutdata)
summary(fm)
ggplot(fm, aes(x= age, y = su))+xlim(30, 70) + geom_jitter(aes(age,su, colour=age)) + facet_grid(sex~race)


I have tried using the facet_wrap() function with ggplot to address the categorical variables, but I am wanting to create just one plot. I was trying a combination of geom_jitter and geom_smooth, but I am not sure how to use geom_smooth() with categorical variables. Any help would be appreciated.



Data: https://github.com/gdlc/STT465/blob/master/gout.txt










share|improve this question





















  • Do you need to set color based on age? Because the most straightforward way might be creating lines / points / whatever and setting their color / linetype / shape based on sex and race
    – camille
    Nov 11 '18 at 20:09











  • I don't need to, no.
    – Hannah
    Nov 11 '18 at 20:14















2














I am trying to produce a plot with age in the x-axis, expected serum urate in the y-axis and lines for male/white, female/white, male/black, female/black, using the estimates from the lm() function.



goutdata <- read.table("gout.txt", header = TRUE)
goutdata$sex <- factor(goutdata$sex,levels = c("M", "F"))
goutdata$race <- as.factor(goutdata$race)

fm <- lm(su~sex+race+age, data = goutdata)
summary(fm)
ggplot(fm, aes(x= age, y = su))+xlim(30, 70) + geom_jitter(aes(age,su, colour=age)) + facet_grid(sex~race)


I have tried using the facet_wrap() function with ggplot to address the categorical variables, but I am wanting to create just one plot. I was trying a combination of geom_jitter and geom_smooth, but I am not sure how to use geom_smooth() with categorical variables. Any help would be appreciated.



Data: https://github.com/gdlc/STT465/blob/master/gout.txt










share|improve this question





















  • Do you need to set color based on age? Because the most straightforward way might be creating lines / points / whatever and setting their color / linetype / shape based on sex and race
    – camille
    Nov 11 '18 at 20:09











  • I don't need to, no.
    – Hannah
    Nov 11 '18 at 20:14













2












2








2







I am trying to produce a plot with age in the x-axis, expected serum urate in the y-axis and lines for male/white, female/white, male/black, female/black, using the estimates from the lm() function.



goutdata <- read.table("gout.txt", header = TRUE)
goutdata$sex <- factor(goutdata$sex,levels = c("M", "F"))
goutdata$race <- as.factor(goutdata$race)

fm <- lm(su~sex+race+age, data = goutdata)
summary(fm)
ggplot(fm, aes(x= age, y = su))+xlim(30, 70) + geom_jitter(aes(age,su, colour=age)) + facet_grid(sex~race)


I have tried using the facet_wrap() function with ggplot to address the categorical variables, but I am wanting to create just one plot. I was trying a combination of geom_jitter and geom_smooth, but I am not sure how to use geom_smooth() with categorical variables. Any help would be appreciated.



Data: https://github.com/gdlc/STT465/blob/master/gout.txt










share|improve this question













I am trying to produce a plot with age in the x-axis, expected serum urate in the y-axis and lines for male/white, female/white, male/black, female/black, using the estimates from the lm() function.



goutdata <- read.table("gout.txt", header = TRUE)
goutdata$sex <- factor(goutdata$sex,levels = c("M", "F"))
goutdata$race <- as.factor(goutdata$race)

fm <- lm(su~sex+race+age, data = goutdata)
summary(fm)
ggplot(fm, aes(x= age, y = su))+xlim(30, 70) + geom_jitter(aes(age,su, colour=age)) + facet_grid(sex~race)


I have tried using the facet_wrap() function with ggplot to address the categorical variables, but I am wanting to create just one plot. I was trying a combination of geom_jitter and geom_smooth, but I am not sure how to use geom_smooth() with categorical variables. Any help would be appreciated.



Data: https://github.com/gdlc/STT465/blob/master/gout.txt







r ggplot2 lm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 '18 at 20:01









Hannah

326




326











  • Do you need to set color based on age? Because the most straightforward way might be creating lines / points / whatever and setting their color / linetype / shape based on sex and race
    – camille
    Nov 11 '18 at 20:09











  • I don't need to, no.
    – Hannah
    Nov 11 '18 at 20:14
















  • Do you need to set color based on age? Because the most straightforward way might be creating lines / points / whatever and setting their color / linetype / shape based on sex and race
    – camille
    Nov 11 '18 at 20:09











  • I don't need to, no.
    – Hannah
    Nov 11 '18 at 20:14















Do you need to set color based on age? Because the most straightforward way might be creating lines / points / whatever and setting their color / linetype / shape based on sex and race
– camille
Nov 11 '18 at 20:09





Do you need to set color based on age? Because the most straightforward way might be creating lines / points / whatever and setting their color / linetype / shape based on sex and race
– camille
Nov 11 '18 at 20:09













I don't need to, no.
– Hannah
Nov 11 '18 at 20:14




I don't need to, no.
– Hannah
Nov 11 '18 at 20:14












2 Answers
2






active

oldest

votes


















3














We can use interaction() to create groupings on the fly and perform the OLS right within geom_smooth(). Here they are grouped on one plot:



ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
geom_smooth(formula = y~x, method="lm") +
geom_point() +
hrbrthemes::theme_ipsum_rc(grid="XY")


enter image description here



and, spread out into facets:



ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
geom_smooth(formula = y~x, method="lm") +
geom_point() +
facet_wrap(sex~race) +
hrbrthemes::theme_ipsum_rc(grid="XY")


enter image description here



You've now got a partial answer to #1 of https://github.com/gdlc/STT465/blob/master/HW_4_OLS.md :-)






share|improve this answer




























    1














    You could probably use geom_smooth() to show regression lines?



    dat <- read.table("https://raw.githubusercontent.com/gdlc/STT465/master/gout.txt", 
    header = T, stringsAsFactors = F)

    library(tidyverse)

    dat %>%
    dplyr::mutate(sex = ifelse(sex == "M", "Male", "Female"),
    race = ifelse(race == "W", "Caucasian", "African-American"),
    group = paste(race, sex, sep = ", ")
    ) %>%
    ggplot(aes(x = age, y = su, colour = group)) +
    geom_smooth(method = "lm", se = F, show.legend = F) +
    geom_point(show.legend = F, position = "jitter", alpha = .5, pch = 16) +
    facet_wrap(~group) +
    ggthemes::theme_few() +
    labs(x = "Age", y = "Expected serum urate level")


    enter image description here






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      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%2f53252688%2fplotting-categorical-variables-ols-in-r%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









      3














      We can use interaction() to create groupings on the fly and perform the OLS right within geom_smooth(). Here they are grouped on one plot:



      ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
      geom_smooth(formula = y~x, method="lm") +
      geom_point() +
      hrbrthemes::theme_ipsum_rc(grid="XY")


      enter image description here



      and, spread out into facets:



      ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
      geom_smooth(formula = y~x, method="lm") +
      geom_point() +
      facet_wrap(sex~race) +
      hrbrthemes::theme_ipsum_rc(grid="XY")


      enter image description here



      You've now got a partial answer to #1 of https://github.com/gdlc/STT465/blob/master/HW_4_OLS.md :-)






      share|improve this answer

























        3














        We can use interaction() to create groupings on the fly and perform the OLS right within geom_smooth(). Here they are grouped on one plot:



        ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
        geom_smooth(formula = y~x, method="lm") +
        geom_point() +
        hrbrthemes::theme_ipsum_rc(grid="XY")


        enter image description here



        and, spread out into facets:



        ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
        geom_smooth(formula = y~x, method="lm") +
        geom_point() +
        facet_wrap(sex~race) +
        hrbrthemes::theme_ipsum_rc(grid="XY")


        enter image description here



        You've now got a partial answer to #1 of https://github.com/gdlc/STT465/blob/master/HW_4_OLS.md :-)






        share|improve this answer























          3












          3








          3






          We can use interaction() to create groupings on the fly and perform the OLS right within geom_smooth(). Here they are grouped on one plot:



          ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
          geom_smooth(formula = y~x, method="lm") +
          geom_point() +
          hrbrthemes::theme_ipsum_rc(grid="XY")


          enter image description here



          and, spread out into facets:



          ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
          geom_smooth(formula = y~x, method="lm") +
          geom_point() +
          facet_wrap(sex~race) +
          hrbrthemes::theme_ipsum_rc(grid="XY")


          enter image description here



          You've now got a partial answer to #1 of https://github.com/gdlc/STT465/blob/master/HW_4_OLS.md :-)






          share|improve this answer












          We can use interaction() to create groupings on the fly and perform the OLS right within geom_smooth(). Here they are grouped on one plot:



          ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
          geom_smooth(formula = y~x, method="lm") +
          geom_point() +
          hrbrthemes::theme_ipsum_rc(grid="XY")


          enter image description here



          and, spread out into facets:



          ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
          geom_smooth(formula = y~x, method="lm") +
          geom_point() +
          facet_wrap(sex~race) +
          hrbrthemes::theme_ipsum_rc(grid="XY")


          enter image description here



          You've now got a partial answer to #1 of https://github.com/gdlc/STT465/blob/master/HW_4_OLS.md :-)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 20:28









          hrbrmstr

          60.1k686148




          60.1k686148























              1














              You could probably use geom_smooth() to show regression lines?



              dat <- read.table("https://raw.githubusercontent.com/gdlc/STT465/master/gout.txt", 
              header = T, stringsAsFactors = F)

              library(tidyverse)

              dat %>%
              dplyr::mutate(sex = ifelse(sex == "M", "Male", "Female"),
              race = ifelse(race == "W", "Caucasian", "African-American"),
              group = paste(race, sex, sep = ", ")
              ) %>%
              ggplot(aes(x = age, y = su, colour = group)) +
              geom_smooth(method = "lm", se = F, show.legend = F) +
              geom_point(show.legend = F, position = "jitter", alpha = .5, pch = 16) +
              facet_wrap(~group) +
              ggthemes::theme_few() +
              labs(x = "Age", y = "Expected serum urate level")


              enter image description here






              share|improve this answer

























                1














                You could probably use geom_smooth() to show regression lines?



                dat <- read.table("https://raw.githubusercontent.com/gdlc/STT465/master/gout.txt", 
                header = T, stringsAsFactors = F)

                library(tidyverse)

                dat %>%
                dplyr::mutate(sex = ifelse(sex == "M", "Male", "Female"),
                race = ifelse(race == "W", "Caucasian", "African-American"),
                group = paste(race, sex, sep = ", ")
                ) %>%
                ggplot(aes(x = age, y = su, colour = group)) +
                geom_smooth(method = "lm", se = F, show.legend = F) +
                geom_point(show.legend = F, position = "jitter", alpha = .5, pch = 16) +
                facet_wrap(~group) +
                ggthemes::theme_few() +
                labs(x = "Age", y = "Expected serum urate level")


                enter image description here






                share|improve this answer























                  1












                  1








                  1






                  You could probably use geom_smooth() to show regression lines?



                  dat <- read.table("https://raw.githubusercontent.com/gdlc/STT465/master/gout.txt", 
                  header = T, stringsAsFactors = F)

                  library(tidyverse)

                  dat %>%
                  dplyr::mutate(sex = ifelse(sex == "M", "Male", "Female"),
                  race = ifelse(race == "W", "Caucasian", "African-American"),
                  group = paste(race, sex, sep = ", ")
                  ) %>%
                  ggplot(aes(x = age, y = su, colour = group)) +
                  geom_smooth(method = "lm", se = F, show.legend = F) +
                  geom_point(show.legend = F, position = "jitter", alpha = .5, pch = 16) +
                  facet_wrap(~group) +
                  ggthemes::theme_few() +
                  labs(x = "Age", y = "Expected serum urate level")


                  enter image description here






                  share|improve this answer












                  You could probably use geom_smooth() to show regression lines?



                  dat <- read.table("https://raw.githubusercontent.com/gdlc/STT465/master/gout.txt", 
                  header = T, stringsAsFactors = F)

                  library(tidyverse)

                  dat %>%
                  dplyr::mutate(sex = ifelse(sex == "M", "Male", "Female"),
                  race = ifelse(race == "W", "Caucasian", "African-American"),
                  group = paste(race, sex, sep = ", ")
                  ) %>%
                  ggplot(aes(x = age, y = su, colour = group)) +
                  geom_smooth(method = "lm", se = F, show.legend = F) +
                  geom_point(show.legend = F, position = "jitter", alpha = .5, pch = 16) +
                  facet_wrap(~group) +
                  ggthemes::theme_few() +
                  labs(x = "Age", y = "Expected serum urate level")


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 '18 at 20:24









                  utubun

                  1,1841711




                  1,1841711



























                      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%2f53252688%2fplotting-categorical-variables-ols-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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo