llewmills
llewmills

Reputation: 3590

Italics within plotmath expression cannot be rendered bold in ggplot

Based on previous posts I worked out how to use expression() to get a string within a string italicised while the rest of the string remains un-italicised. The problem is that element_text(face = "bold") does not work on strings inside expression().

ggplot(iris, aes(x = Sepal.Width)) + 
       geom_histogram(bins = 10) + 
       ylab(expression(paste("% of group ", italic("n")))) + 
       xlab("Actual Treatment") + 
       theme(axis.title.x = element_text(face = "bold"),
             axis.title.y = element_text(face = "bold"))

To get around this I wrapped the expression() in bold() like so

ggplot(iris, aes(x = Sepal.Width)) + 
       geom_histogram(bins = 10) + 
       ylab(expression(bold(paste("% of group ", italic("n"))))) + 
       xlab("Actual Treatment") + 
       theme(axis.title.x = element_text(face = "bold"))

But alas the italicised n remains unbolded. Any ideas?

Upvotes: 2

Views: 927

Answers (2)

Delphine
Delphine

Reputation: 11

ggplot(iris, aes(x = Sepal.Width)) + 
geom_histogram(bins = 10) + 
labs(y=expression(bold(paste("% of group ", bolditalic("n"))),
x="Actual Treatment"))+
theme(axis.title.x = element_text(face = "bold"))

Upvotes: 0

user10505710
user10505710

Reputation: 56

expression(bold("% of group ")*bolditalic("n"))

Upvotes: 4

Related Questions