Hello in this lecture we're going to learn how to build scatterplot chart with a plot to do it.
I'm going to use.
It's called H W and it contains information about the page and the weight of 70 people male and female.
So the hate is expressed in centimeters the way it is pressed in kilograms and we are going to create
a scatterplot with the variables hate and wage first class load the package Juji plot to and then create
the plot using the function g point.
Here we must define our that AGW and the aesthetics function.
We have to specify the variables represented on the X and the y axis.
So on the x axis we will have the hate.
And on the y axis with have the waves and because the variable hates on the x K is continuous.
I used the functions K and X continues to define the limits for the variable page.
There are one hundred and fifty and one hundred and ninety three because these are the minimum and the
maximum page in the data frame were more or less that Thuram this fall.
And here our scatterplot as you can note is the way it depends on the on the hate the generally the
higher people tend to have a greater weight as you can see here on the chart.
OK.
Now let's build a plaster scatterplot by gender.
So we are going to have to add two groups of dots for the male and female categories.
First of all we are going to store the variable gender in the data pray age Doblin.
So these are people who are going to store it in a new variable called LGT a new factor.
OK.
And now we're going to build our scatterplot with points of different color how to do that here.
And they asserted that it's function.
We enter a new option color widget equal to l g d.
So the program now that they it has to differentiate the color of the of the points by gender by the
variable.
LG D.
The limit is the same.
What is that from this.
So you can notice here that the blue dot correspond to the male group the male subject and the red dot
correspond to the female object.
There's a program added a legend automatically because we used the opposite color in this function and
that is if you want to get points of different shapes instead of different colors you will use.
Here they are in shape instead of color.
So the options shape will be equal to LG The
gray So here you may notice in the legend you have a dog for the female group and triangles for the
May group.
It's harder to to to see a little bit so different colors is better.
And if you want to get points of both different shapes and colors of course you can have it.
So here is the aesthetic text function story.
You and I are both options the shape and color and both are equal to how g d.
So the shape and the color of the dark will be different depending on the gender.
The category of the grouping variable Let's round this.
OK.
So here for the female group we have blue dots while for the male group we have blue triangles
OK.
Now let see how to add a trend line to our scatterplot that a prettier more complex task.
First of all let's create a linear mode so we can wait for the dependent variable and hate the explainer
the independent variable.
We'll do that with the powerful Ahlam which stands for lenience mode of.
So here weight is dependent babba hate you.
The independent variables.
They are separated by Tilda and our data frame is age gobbler.
You and we are going to tour this Modo in a variable called molto OK.
So let's run this.
That's clear the console a bit first and then let's bring it over.
Or more dough in half of it contains the coefficients of the regression Modell with weights and with
the weight and the page.
So the intercept is minus sixty two point two and the coefficient of the two variables the independent
variable is the real point seventy nine.
Now let's get the minimum and the maximum height hate's.
Sorry.
So we store them in two variables mean age and age and then we're going to create a new vector containing
their limits.
We are going to call it hate.
So using the C function and the variables minimum and maximum age that's printed.
So here we have a factor with the minimum wage.
One hundred fifty point seven.
And the market on page one hundred ninety two point three OK.
And now based on our linear mode of code although you remember we are going to produce the weight based
on the Haig's.
So in this Foxton predicts we are going to define them to specify of course our model and then we are
going to specify the dependent variable.
The independent variable takes the explainer.
Sorry but as a data frame because that's how the produce Ponson the fire works.
It requires that data operate here.
So I'm using the data brain function with the argument the variable hate the independent variable.
And I'm going to store the prediction in a new vector called fact OK now let's and the facts are straight.
Here we have of course two values.
These are the predicted ways for the two hates minimum and maximum so for the minimum wage we have a
predicted wage of
story fifty seven point five kilograms.
And for the maximum wage we have a predicted wage of ninety point six kilograms.
Now let's create a date frame with two factors.
Hate and fear.
So the maximum Hayton the minimum wage and their corresponding production with a function date of Crame.
And we are going to create this new data frame and point because there will be the end point of our
trend line.
So let's create the data frame and point and let me do it now.
So here in that column page we have the minimum and the maximum page and here in the column that we
have the corresponding predicted waves.
OK.
So we're going to use the data in that data frame the information in the data frame to draw our line.
Our trend line.
So now we can build the counterplot with the trend line.
So here I am using of course the farm and genome point to draw the cattle prod to just like in the previous
examples.
And in order to draw the line I will use the function thingee on line the date frame either quarter
and points and here the ecstatic function I specify the variables represented on the x and y axis.
So on the variable on the axis X excuse me you will have the page page from the endpoints data frame
and on the x y I would have the column fit the variable speed again in this data frame and points OK.
And I will make this line in the rad and with the size of one that's from this
fine.
So here you have this kind of plot with the line with a trap line draw in the middle of the points that
it is used for when you want to know whether the relationship between the two variables is approximately
linear.
For example if you want to compute the Pearson correlation between the variables so that it's about
the count of all.
Thank you.