2.4 C
London
Friday, December 20, 2024
HomeTidyverse in Rggplot2 in RHow to Change Point Size in ggplot2 (3 Examples)

How to Change Point Size in ggplot2 (3 Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the size argument to change the size of points in a ggplot2 scatterplot:

some_ggplot +
  geom_point(size=1.5)

The default size is 1.5 but you can decrease or increase this value to make the points smaller or larger.

The following examples show how to use each method in practice with the built-in mtcars dataset in R.

For reference, here’s what a ggplot2 scatterplot looks like using the default size:

library(ggplot2)

ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point()

Example 1: Increase Point Size in ggplot2

The following code shows how to create a scatterplot in ggplot2 and increase the point size by using the size argument:

library(ggplot2)

#create scatterplot with increased point size
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point(size=5)

ggplot2 increase point size

Notice that the points are much larger than the default size.

Example 2: Decrease Point Size in ggplot2

The following code shows how to create a scatterplot in ggplot2 and decrease the point size by using the size argument:

library(ggplot2)

#create scatterplot with decreased point size
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point(size=0.5)

ggplot2 decrease point size

Notice that the points are much smaller than the default size.

Example 3: Adjust Point Size Based on Variable

The following code shows how to create a scatterplot in ggplot2 and adjust each point size based on the value of another variable in the mtcars dataset called qsec:

library(ggplot2)

#create scatterplot with point size based on value of qsec
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point(aes(size=qsec))

The size of each point is now dependent on the value of the qsec variable.

Notice that ggplot2 also automatically adds a legend on the right side of the plot to help you understand how the value of the qsec variable is mapped onto the size of each point.

Note: You can find the complete documentation for the geom_point() function here.

Additional Resources

The following tutorials explain how to perform other common operations in ggplot2:

How to Change Point Shape in ggplot2
How to Change the Legend Title in ggplot2
How to Rotate Axis Labels in ggplot2
How to Fix in R: could not find function “ggplot”

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories