Scatter Plot

Sample Dataset

Exercises

Create a basic scatter plot with height_cm on the x-axis and weight_kg on the y-axis.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg) +
    geom_point() +
    labs(title = "Scatter Plot of Height vs. Weight", 
             x = "Height (cm)", 
             y = "Weight (kg)") +
    theme_minimal()  # free style

Create a scatter plot of height_cm vs. weight_kg where the points’ size represents the age_years variable.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg, size = age_years) +
    geom_point(alpha = 0.7) +
    labs(title = "Bubble Plot of Height vs. Weight with Age Size", 
             x = "Height (cm)", 
             y = "Weight (kg)", 
          size = "Age (years)") +
    theme_minimal() # free style

Create a scatter plot of height_cm vs. weight_kg and color the points based on the gender variable.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg, color = gender) +
    geom_point() +
    labs(title = "Scatter Plot of Height vs. Weight Colored by Gender", 
             x = "Height (cm)", 
             y = "Weight (kg)") +
    theme_minimal() # free style

Create a bubble plot where point color represents the gender variable and size represents age_years.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg, size = age_years, color = gender) +
    geom_point(alpha = 0.6) +
    labs(title = "Bubble Plot of Height vs. Weight Colored by Gender", 
             x = "Height (cm)", 
             y = "Weight (kg)", 
             size = "Age (years)") +
    theme_minimal() # free style

Create a scatter plot of height_cm vs. weight_kg and add a linear regression line using geom_smooth(method = "lm").

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg) +
    geom_point() +
    geom_smooth(method = "lm", color = "blue") +
    labs(title = "Scatter Plot with Regression Line", 
             x = "Height (cm)", 
             y = "Weight (kg)") +
    theme_minimal() # free style

Create a scatter plot of height_cm vs. weight_kg and add a smooth line using geom_smooth() with the default method.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg) +
    geom_point() +
    geom_smooth(color = "red") +
    labs(title = "Scatter Plot with Smoothing Line", 
             x = "Height (cm)", 
             y = "Weight (kg)") +
    theme_minimal() # free style

Create a scatter plot of height_cm vs. weight_kg and facet the plot by city using facet_grid.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg) +
    geom_point() +
    facet_grid(~ city) +
    labs(title = "Scatter Plot of Height vs. Weight Faceted by City",
             x = "Height (cm)", 
             y = "Weight (kg)") +
    theme_minimal()  # free style

Create a scatter plot of height_cm vs. weight_kg and change the point shape based on the gender variable.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg, shape = gender) +
    geom_point(size = 3) +
    labs(title = "Scatter Plot with Different Shapes by Gender", 
             x = "Height (cm)", 
             y = "Weight (kg)") +
    theme_minimal() # free style

Create a scatter plot where height_cm vs. weight_kg points are colored by gender and point shape varies based on the city.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg, shape = gender, color = city) +
    geom_point(size = 3) +
    labs(title = "Scatter Plot Colored by City and Shaped by Gender",
             x = "Height (cm)", 
             y = "Weight (kg)") +
    theme_minimal() # free style

Create a scatter plot of height_cm vs. weight_kg with points colored by gender, add a regression line, and use facet_grid to separate by city.

Solution:

data |> 
  ggplot() +
    aes(x = height_cm, y = weight_kg, color = gender) +
    geom_point() +
    geom_smooth(method = "lm") +
    facet_grid(~ city) +
    labs(title = "Scatter Plot with Regression Line, Colored by Gender, Faceted by City", 
             x = "Height (cm)", 
             y = "Weight (kg)") +
    theme_minimal() # free style