Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to italicize one category in a legend in ggplot2

For my legend in ggplot2 I have 2 categories, how do I italicize only 1 category and not the other?

As an example, consider the following plot. How can I set "Manual" in italics?

library(ggplot2)

ggplot(data = mtcars, aes(x = as.factor(am), fill = as.factor(am))) + 
  geom_bar() + 
  scale_fill_discrete(
    "Transmission",
    breaks = c(0, 1),
    labels = c("Automatic", "Manual")
  )

Created on 2020-01-01 by the reprex package (v0.3.0)

like image 544
Eswim Avatar asked Feb 08 '18 14:02

Eswim


People also ask

How do I specify legend titles in ggplot2?

Another way to change legend titles is to use guides() function in ggplot2. Here, guides() function can take two legend titles as arguments. We use guide_legend() to specify the new title we want one for size and other for color.

How do I change the legend text in Ggplot?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do you write italics in R?

To write text in italic font, use a single underscore or asterix before and after the text. To write text in bold font, use a double asterix or underscores before and after the text.


1 Answers

You can use expression and italic to create italicized text on labels.

library(ggplot2)

ggplot(data = mtcars, aes(x = as.factor(am), fill = as.factor(am))) + 
  geom_bar() + 
  scale_fill_discrete(
    "Transmission",
    breaks = c(0, 1),
    labels = c("Automatic", expression(italic("Manual")))
  )

Created on 2020-01-01 by the reprex package (v0.3.0)

like image 63
r.bot Avatar answered Oct 12 '22 01:10

r.bot