2

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?

llewmills
  • 2,959
  • 3
  • 31
  • 58

2 Answers2

4
expression(bold("% of group ")*bolditalic("n"))
0
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"))
Delphine
  • 11
  • 1
  • 3
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Aug 02 '20 at 08:56