How to changes words in a charts title based on condition?

I would like the title of my plot to change based on if the supply is over or under the estimate. The estimate of the supply is in DF$Supply.. If it is positive its over the estimate and if it is negative it’s under the estimate. How can i set up the condition to check the DF$Supply and chose the right title.

If the supply is OVER the estimate i would like the title to be this:

labs(title=paste("In 2021-2030 the supply is over the estimate by", DF$Supply)

If the supply is UNDER the estimate i would like the title to be this:

labs(title=paste("In 2021-2030 the supply is under the estimate by", DF$Supply*-1)

If the supply is equal to the estimate the title is (this means that DF$Supply = 0):

labs(title="In 2021-2030 the supply matches the estimate")

>Solution :

Im not sure if this is going to work, its always better when you provide the dataset…

labs(title = case_when(
      DF$Supply >= 1 ~ paste("In 2021-2030 the supply is over the estimate by", DF$Supply),
      DF$Supply < 0 ~ paste("In 2021-2030 the supply is under the estimate by", DF$Supply*-1),
      DF$Supply == 0 ~ "In 2021-2030 the supply matches the estimate"
    ))

Leave a Reply