Please execute the following code snippet before proceeding with the rest of the exercises.
set.seed(040197)
x = 1:100 + rnorm(100, mean = 6, sd = 19)
y = 1:100
x
and y
vectors generated earlier.x
and y
axes as “X
values” and “Y values”, and add the title “Making a Scatter Plot
Example”.mtext()
and paste()
(or
paste0()
, search for the difference between the two) to add
margin text at the top of the plot (“Today is” followed by the current
date).paste()
as the ‘text’ argument in
mtext()
.mtext(side = 3, text = paste(...))
;
to retrieve the current date, use the Sys.Date()
function,
which can be directly inserted into the paste()
function.cor()
function calculates the correlation between
two vectors. Pearson’s correlation measures the linear relationship
(dependence) between two variables, X and Y.cor()
function with the x
and
y
variablesmtext()
, cor()
, and
paste()
to display the correlation coefficient on your
scatter plot.round()
function. Then, explore what floor()
and ceiling()
do.pch
argument in your
plot()
command and explore the col=
and
bg=
parameters accordingly.x
variable. A histogram is a
graphical representation of the data distribution.y
variable. Remember to set
notch = TRUE
.x
and y
vectors on
the same plot, using two different colors for each.horizontal = TRUE
argument.Create multiple plots using
par(mfrow = c(2, 1))
:
Run par(mfrow = c(2, 1))
to set up the plotting
layout.
Create a boxplot of the y
variable.
Create a histogram of the x
variable.
par(mfrow = c(1, 2))
PDF
.You can create a scatter plot that shows the density of points instead of plotting the points themselves. This is useful when you want to see if your points accumulate around certain values or if you have too many points, making it difficult to distinguish their precise locations.
By using the smoothScatter()
function instead of
plot(x, y)
, you can visualize the densities. Try it
out!
Then, use
colramp = colorRampPalette(c("white", "blue", "green", "yellow", "red"))
to apply custom colors.
Feel free to adjust the colors to your liking and save the plot as a
PDF.