4.5 C
London
Friday, December 20, 2024
HomeTidyverse in Rggplot2 in RHow to Add Label to geom_hline in ggplot2

How to Add Label to geom_hline 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 basic syntax to add a label to a horizontal line in ggplot2:

+ annotate("text", x=9, y=20, label="Here is my text")

The following examples show how to use this syntax in practice.

Example 1: Add Label to geom_hline

The following code shows how to add a label to a horizontal line in ggplot2:

library(ggplot2)

#create data frame 
df frame(x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with horizontal line at y=20
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_hline(yintercept=20) +
  annotate("text", x=9, y=20.5, label="Some text")

Add label to geom_hline in ggplot2

Example 2: Add Customized Label to geom_hline

The following code shows how to use the size and color arguments to add a label with a custom size and color to a horizontal line in ggplot2:

library(ggplot2)

#create data frame 
df frame(x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with horizontal line at y=20
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_hline(yintercept=20) +
  annotate("text", x=10, y=21.5, label="Some text", size=15, color="blue")

Example 3: Add Multiple Labels to geom_hline

The following code shows how to use the annotate() function multiple times to add multiple labels to a horizontal line in ggplot2:

library(ggplot2)

#create data frame 
df frame(x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with horizontal line at y=10
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_hline(yintercept=20) +
  annotate("text", x=10, y=19, label="Some text", size=15, color="blue") +
  annotate("text", x=10, y=21, label="More text", size=9, color="red")

Feel free to use the annotate() function as many times as you’d like to add as many labels as you’d like to the plot.

Additional Resources

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

How to Add Label to geom_vline in ggplot2
How to Add an Average Line to Plot in ggplot2
How to Change Line Colors 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