6.2 C
London
Thursday, December 19, 2024
HomeTidyverse in Rggplot2 in RHow to Adjust Line Thickness in Boxplots in ggplot2

How to Adjust Line Thickness in Boxplots in ggplot2

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 following methods to adjust the thickness of the lines in a boxplot in ggplot2:

Method 1: Adjust Thickness of All Lines

ggplot(df, aes(x=x, y=y)) + 
  geom_boxplot(lwd=2)

Method 2: Adjust Thickness of Median Line Only

ggplot(df, aes(x=x, y=y)) + 
  geom_boxplot(fatten=4)

The following examples show how to use each method in practice with the following data frame in R:

#make this example reproducible
set.seed(1)

#create data frame
df A', 'B', 'C'), each=100),
                 points=c(rnorm(100, mean=10),
                          rnorm(100, mean=15),
                          rnorm(100, mean=20)))

#view head of data frame
head(df)

  team    points
1    A  9.373546
2    A 10.183643
3    A  9.164371
4    A 11.595281
5    A 10.329508
6    A  9.179532

Note: We used the set.seed() function to ensure that this example is reproducible.

Example 1: Create Boxplot with Default Line Thickness

The following code shows how to create a boxplot to visualize the distribution of points grouped by team, using the default line thickness:

library(ggplot2)

#create box plots to visualize distribution of points by team
ggplot(df, aes(x=team, y=points)) + 
  geom_boxplot()

Example 2: Create Boxplot with Increased Line Thickness

The following code shows how to create a boxplot to visualize the distribution of points grouped by team, using the lwd argument to increase the thickness of all lines in the boxplot:

library(ggplot2)

#create box plots with increased line thickness
ggplot(df, aes(x=team, y=points)) + 
  geom_boxplot(lwd=2)

ggplot2 adjust line thickness in boxplots

Notice that the thickness of each of the lines in each boxplot has increased.

Example 3: Create Boxplot with Increased Line Thickness of Median Line Only

The following code shows how to create a boxplot to visualize the distribution of points grouped by team, using the fatten argument to increase the thickness of the median line in each boxplot:

library(ggplot2)

#create box plots with increased median line thickness
ggplot(df, aes(x=team, y=points)) + 
  geom_boxplot(fatten=4)

ggplot2 increase line thickness of median line in boxplot

Notice that only the thickness of the median line in each boxplot has increased.

Feel free to play around with both the lwd and fatten arguments in geom_boxplot() to create boxplots that have the exact line thickness you’d like.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Change Axis Labels of Boxplot in ggplot2
How to Create a Grouped Boxplot in ggplot2
How to Label Outliers in Boxplots in ggplot2

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