Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bolded axis with subscripted character using ggplot2 in R

Tags:

r

ggplot2

I am trying to create a bold axis label with a subscripted character using ggplot2 in R.

I have already tried the following, but the subscripted number seems to be 'unbolded' (see image below).

Current results

It appears that the 2 after 'log' on the y-axis is not in bold. However, the answer posted here (Trying to bold y axis in ggplot) seems to suggest that it is. Is this just the way ggplot2 makes it look?

Here is a bit of code to replicate the problem (if it is indeed a problem) using a simpler data set and less formatting of the plot output (which shouldn't make a difference to the axis labels):

library(ggplot2)  

dat <- data.frame(x = rnorm(100), y = rnorm(100))

ggplot(dat, aes(x=x,y=y)) +
  geom_point() +
  ylab(expression(bold('Coefficient estimate for log'[2]*' FPRS variable'))) + 
  xlab(expression(bold('This is what a normal 2 looks like'))) 

I have also tried the following (which gives the same result):

labs(y = expression(bold('Coefficient estimate for log'[2]*' FPRS variable')))

Thanks in advance for the help!

like image 932
arranjdavis Avatar asked Sep 12 '25 00:09

arranjdavis


2 Answers

Using the latex2exp you can bold text easily with wrapping the text with '\\textbf{}' and use LaTeX for Log_2

library(ggplot2) 
library(latex2exp)

dat <- data.frame(x = rnorm(100), y = rnorm(100))

ggplot(dat, aes(x=x,y=y)) +
  geom_point() +
  ylab(TeX('\\textbf{Coefficient estimate for $log_2$ FPRS variable}')) + 
  xlab(TeX('\\textbf{This is what a normal 2 looks like}'))

Hope this is helpful!

like image 68
Bensstats Avatar answered Sep 14 '25 14:09

Bensstats


The above answer has worked for me in the past, but for reasons that I may never fully understand, it doesn't always work. In these instances, I had to use something like the following:

ylab(expression(bold("Coefficient estimate for log"["2"]*" FPRS variable")))

Note the quotations around the subscripted 2. This comes from this answer to a different question on this wonderful website.

like image 20
arranjdavis Avatar answered Sep 14 '25 14:09

arranjdavis