R Scatter Plot


Scatter Plots

You learned in the Plot chapter that the plot() function is used to arrange numbers against each other.

"Scatter structure" is a type of structure that is used to show the relationship between two dynamic numbers, and arranges one dot for each view.

Requires two vectors of the same length, one of the x (horizontal) axis and one of the y (vertical) axis:


Example
x <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)

plot(x, y)

Result

Note in the example above should show the effect of 12 passing vehicles.

That may not be clear to anyone who sees the graph for the first time, so let's add a different title and label to better define the scatter plot:


Example
x <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)

plot(x, y, main="Observation of Cars", xlab="Car age", ylab="Car speed")

Result

Again, the observation in the example above is the result of 12 passing cars.

The x-axis shows how old the car is.

The y-axis shows the speed of the car as it passes.

Is there a relationship between the comments?

It looks like a new car driving at high speed but that may be a coincidence, as we only registered 12 cars.


Compare Plots

In the example above, there seems to be a link between car speed and age, but what if we plan to see another day? Will the scattering plot tell us anything else?

To compare a building with another building, use the points() function:


Example

Draw two plots on the same figure

# day one, the age and speed of 12 cars:
x1 <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y1 <- c(99,86,87,88,111,103,87,94,78,77,85,86)

# day two, the age and speed of 15 cars:
x2 <- c(2,2,8,1,15,8,12,9,7,3,11,4,7,14,12)
y2 <- c(100,105,84,105,90,99,90,95,94,100,79,112,91,80,85)

plot(x1, y1, main="Observation of Cars", xlab="Car age", ylab="Car speed", col="red", cex=2)
points(x2, y2, col="blue", cex=2)

Result

Note: In order to see the difference in contrast, you must assign different colors to the properties (using the col parameter). Red represents 1 day values, while blue represents day 2. Note that we also add a cex parameter to increase the size of the dots.


Viewpoint: By comparing these two episodes, I think it is safe to say that they both give us the same conclusion: if a car is new, it drives faster.