🧀Scatter Plot
We can use ggplot2 package to create a scatterplot.
library('palmerpenguins')
library('ggplot2')Penguins dataset has following type of data.

Let's create a scatterplot to see the relation between flipper_length_mm and body_mass_g from this dataset.
ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point()
Change the color of the points.
ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(color = "green")Here, we have made the point's color green by specifying the name of the color by color argument inside geome_point function.

Color data points based on a column
Let's say we want to color the data points based on penguin species.
ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species))Under geom_point function we added the code aes(color = species) which has changed the color of the point based on the species type from the species column.

Change the shape of a data point based on a column
Let's change the data point shape based on the species column from the penguins dataset.
ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species))Here, shape = species argument under geom_point function has changed the data point's shape according to the different species of penguins.

Divide the plot based on groups from a column.
Let's say we want the create different scatter plots for each species. For this purpose, we can subset our data into smaller sections and get graphs for each one.
ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species)) +
facet_wrap(~species)Here, facet_wrap(~species) functions sub-sectioned the plot based on different species of penguins.

Add a title to the plot
ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species)) +
facet_wrap(~species) +
labs(title = "Penguins: Body Mass vs. Flipper Length")Here, we added the title of the plot by labs function.

Last updated