Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making only a part of the legend title bold in R

Tags:

r

legend

I have a legend title spread over two lines, I would like to make only the first line bold. I have the following code:

pre_legend_title = expression(paste(bold("PRE-ELECTION HATE CRIME RATES"),
                                    "\nAverage annual hate crimes per 100,000 residents, 2010-15"))

However, when I try adding it to my ggplot using:

labs(fill = pre_legend_title)

The title doesn't break into two lines despite using \n. Is there another way to do this?

like image 282
Jin Yu Li Avatar asked Nov 25 '25 09:11

Jin Yu Li


1 Answers

As far as I know, expression doesn't recognize linebreak characters. However, you can use the atop function:

library(ggplot2)

pre_legend_title = expression(atop(bold("PRE-ELECTION HATE CRIME RATES"),
                                   "Average annual hate crimes per 100,000 residents, 2010-15"))

ggplot(mtcars, aes(mpg, wt, fill=factor(cyl))) + 
  geom_point(shape=21) +
  labs(fill = pre_legend_title)

enter image description here

like image 101
eipi10 Avatar answered Nov 27 '25 22:11

eipi10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!