-2

I have this data:

# R> smokeyes_male

    LungCap.cc. Age..years. Height.inches. Smoke Gender Caesarean
211       6.575          10           63.2   yes   male       yes
220       7.000          10           62.8   yes   male       yes
252       9.350          11           71.2   yes   male        no
258       7.925          11           67.1   yes   male        no
280      10.275          11           72.2   yes   male        no
285       8.925          11           65.6   yes   male       yes
305       6.450          12           61.0   yes   male       yes
345       8.000          12           64.5   yes   male        no
370       9.750          13           72.8   yes   male        no
371       8.025          13           66.2   yes   male        no

I need to plot a relationship between lung capacity and age with respect to the height of the person or whether the person is Caesarean or not.

I get the following error:

library(ggplot2)
smoker_male_graph=ggplot(smokeyes_male,aes(x=smokeyes_male$age,y=smokeyes_male$LungCap.cc.)) + geom_point()
smoker_male_graph + facet_wrap(~smokeyes_male$Height.inches.)

Error: Aesthetics must be either length 1 or the same as the data (33): x, y

Here is the code:

smokeyes_male <- structure(list(LungCap.cc. = c(6.575, 7, 9.35, 7.925, 10.275, 
                                                8.925, 6.45, 8, 9.75, 8.025), Age..years. = c(10L, 10L, 11L, 
                                                                                              11L, 11L, 11L, 12L, 12L, 13L, 13L), Height.inches. = c(63.2, 
                                                                                                                                                     62.8, 71.2, 67.1, 72.2, 65.6, 61, 64.5, 72.8, 66.2), Smoke = structure(c(1L, 
                                                                                                                                                                                                                              1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "yes", class = "factor"), 
                                Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                     1L), .Label = "male", class = "factor"), Caesarean = structure(c(2L, 
                                                                                                                      2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L), .Label = c("no", "yes"
                                                                                                                      ), class = "factor")), .Names = c("LungCap.cc.", "Age..years.", 
                                                                                                                                                        "Height.inches.", "Smoke", "Gender", "Caesarean"), class = "data.frame", row.names = c("211", 
                                                                                                                                                                                                                                               "220", "252", "258", "280", "285", "305", "345", "370", "371"
                                                                                                                                                        ))
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    well based on your example, it should be `smokeyes_male$Age..years.` not `smokeyes_male$age`, and the first one works fine for me. And you do not need to use `smokeyes_male$` each time, just use the variable name – rawr May 12 '18 at 17:07

1 Answers1

1

Your code is basically correct, it just needs a bit of cleaning up. You can include Caesarean as a color aesthetic.

ggplot(smokeyes_male, aes(x = Age..years., y = LungCap.cc., color = Caesarean)) + 
  geom_point() + 
  facet_wrap(~Height.inches.)

facet plot

From a data visualization standpoint, however, I'm not sure how informative this plot is, given that your facet captures individual heights. That means that, unless two people have the same height, you're only getting one point on each subplot. (Perhaps that's your intent, but it does seem like a curious way to visually explain these data.)

Community
  • 1
  • 1
andrew_reece
  • 20,390
  • 3
  • 33
  • 58