Write R code to create a scatter plot using the vectors height <- c(150, 160, 170, 180) and weight <- c(50, 60, 65, 70). Use base R's plot() function to create a scatter graph with the main title as "Height vs Weight", the X axis label as "Height" and the Y axis label as "Weight". The col value should be "red" and the pch should be 19
Scatter plots are used to visualize the relationship between two numeric variables. In R, you can use the base plot()
function or ggplot2
for more advanced plotting.
# Sample data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 1, 5, 3)
# Create a scatter plot
plot(x, y, main = "Basic Scatter Plot", xlab = "X Values", ylab = "Y Values", col = "blue", pch = 19)
library(ggplot2)
df <- data.frame(x = x, y = y)
ggplot(df, aes(x = x, y = y)) +
geom_point(color = "red", size = 3) +
ggtitle("Scatter Plot with ggplot2") +
theme_minimal()
Scatter plots help identify correlations, trends, and outliers in your data.