Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...