Simple Scatter Plot in R with ggplot2

Scatter plot is a great data visualization tool to visualize relationship between two quantitative variables. We will see an example to make a simple scatter plot in R with ggplot2. Let us first load the ggplot2 package.
library(tidyverse)
Let us create a data set and store it in a data frame.
set.seed(42)
x <- rnorm(100, mean=5, sd=20)
y <- x + rnorm(100, mean=1, sd=20)
df <- data.frame(x=x, y=y)
Let us use the above dataframe to make scatter plot with ggplot2 in R. In ggplot2, geom_point() function helps us make scatter plot. After defining the aesthetics for ggpot2, we can add geom_point() to make scatter plot.
df %>%
   ggplot(aes(x=x,y=y)) + geom_point()
Here we have the simple scatter plot that we just made.
Simple Scatter Plot in R with ggplot2
Are you making a scatter plot with a lots of data points? Then your scatter plot might suffer from overplotting. Check out this post Scatter plots with transparent data points to make a better scatter plot without overplotting.
Exit mobile version