0

Imagine you got 3 variables :

gestation of the mom
height of the mom
weight of the baby at birth

my 2 variables x are :

gestation of the mom
height of the mom

and my variable y is :

weight of the baby at birth

I would like to got a graphics matrix who explains weight of the baby at birth in function of gestation of the mom and weight of the baby at birth in function of height of the mom

I did it :

pairs((baby$bwt~baby$gestation+baby$age))

I obtains a graphic matrix like on picture :matrix_picture

But i would like to know how i can got only y in function of x1 and y in function of x2 because on my picture I got all, in others terms, i would like to obtain only the first line of my picture.

thanks for reading me

EDIT : [matrix2_picture][2]

As you can see, on my absciss i got always same value ( 0 - 300) but i would like to got better value to got a better visualisation on each graphics, for example for age, i can't got 200 or 300, so i would like to got in absciss 10 m and 50 max for example

thanks

EDIT2:

[matrix3][3]

Just a last question, if I want get same thing than on the picture, how I can do it with ggplot

First is gestation of the mom in function of the weight of baby at birth, second is age of the mom in function of the weight of baby at birth and last is height of the mom in function of weigh of baby at birth

I did it :

df3 <- reshape2::melt(baby, "bwt")
 
 ggplot(df3, aes(x=bwt, y=value)) +
   geom_point() + facet_grid(.~variable,scales="free") 

But I obtain it :

[matrix3][4]

Or you can see my ordinate is always same, not like when I used pairs.

thanks a lot !!! [2]: https://i.stack.imgur.com/jppCJ.png [3]: https://i.stack.imgur.com/TnEBe.png [4]: https://i.stack.imgur.com/BPOUP.png

Last edit :

Do u know how we can do the same things but only for redidus of each variable A little bit like the function pairs() but pairs with residus

reg=lm(formula=baby$bwt~baby$weight+baby$gestation+baby$age)
summary(reg)
plot(reg)

I would like to have residus of baby$bwt in function of theses 3 variables( weight , gestation, age)

léo
  • 5
  • 3

1 Answers1

0

For what i know, there isn't a solution using pairs. There are several other options, the one i know uses ggplot2.

First generating some dummy data:

df <- data.frame(
  `gestation of the mom` = rnorm(20,300,30),
  `height of the mom` = rnorm(20,170,10),
  `weight of the baby at birth` = rnorm(20,50,5))

>df
gestation.of.the.mom height.of.the.mom weight.of.the.baby.at.birth
1              304.9339          165.7853                    52.92590
2              219.7718          185.3528                    43.06043
3              310.6279          166.5677                    56.19357
4              278.8190          179.8276                    54.33385
5              247.4760          186.6949                    51.95354

Then reshaping the data frame for ggplot:

df2 <- reshape2::melt(df, "weight.of.the.baby.at.birth")

>df2
weight.of.the.baby.at.birth             variable    value
1                     52.92590 gestation.of.the.mom 304.9339
2                     43.06043 gestation.of.the.mom 219.7718
3                     56.19357 gestation.of.the.mom 310.6279
4                     54.33385 gestation.of.the.mom 278.8190
5                     51.95354 gestation.of.the.mom 247.4760
                              ...
21                    52.92590    height.of.the.mom 165.7853
22                    43.06043    height.of.the.mom 185.3528
23                    56.19357    height.of.the.mom 166.5677
24                    54.33385    height.of.the.mom 179.8276
25                    51.95354    height.of.the.mom 186.6949

Then plotting:

library(ggplot2)
ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
  geom_point() + facet_grid(.~variable)

Output:

enter image description here

You can find other answers in: Pairs scatter plot; one vs many, and Plot one numeric variable against n numeric variables in n plots.

EDIT1:

To make the scales be different, add the scales="free" argument to facet_grid:

ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
  geom_point() + facet_grid(.~variable, scales="free")

Output:

enter image description here

EDIT2:

As you want the fixed variable to be your x axis, you need to change the place of variable in facet_grid:

ggplot(df2, aes(x=value, y=weight.of.the.baby.at.birth)) +
  geom_point() + facet_grid(variable~., scales="free")

Output:

enter image description here

EDIT3:

Creating the model:

reg = lm(df$weight.of.the.baby.at.birth ~ df$gestation.of.the.mom + df$height.of.the.mom)

Adding a column with the residuals (before reshaping), and then reshaping:

df$resid = resid(reg)

df2 <- reshape2::melt(df, c("weight.of.the.baby.at.birth","resid"))

Plotting:

ggplot(df2, aes(x=value, y=resid)) +
  geom_point() + facet_grid(.~variable, scales="free")

Output:

enter image description here

  • thanks man it worked, but I got an other problems that I did in my new answer, but for real thanks !! – léo Oct 24 '20 at 17:53
  • You're welcome. But add that new information in this comment section or edit your original post, not by creating an answer. Now, can you explain better what you mean by "But problems is about graduations , I would like got a better graduation on matrix"? – Ricardo Semião e Castro Oct 24 '20 at 17:58
  • I added the solution :). – Ricardo Semião e Castro Oct 24 '20 at 18:08
  • thanks !! So, the results I show you is a graphics column matrix for you ? Because I'm not sure at 100% – léo Oct 24 '20 at 18:16
  • Is there a formal definition to "graphics column matrix"? For me that result is several graphs organized in a matrix so i'd call it that yeah. If it solved your problem, don't forget to accept the answer to mark the post as solved. – Ricardo Semião e Castro Oct 24 '20 at 18:19
  • yes don't worry I will, but just I want know a last thing and I stop ^^ anyways thanks a lot ! I edit again my answer – léo Oct 24 '20 at 18:32
  • No problem, if you have further questions don't mind asking. Check if the edit2 is what you want. – Ricardo Semião e Castro Oct 24 '20 at 18:45
  • thanks but i don't unterstand, why it works when you do it : " f$resid = resid(reg)" – léo Oct 24 '20 at 19:29
  • When you have a data frame you can add columns to it by doing that, as long as the vector you're passing has the same number of elements as the data has of rows. – Ricardo Semião e Castro Oct 24 '20 at 19:32