0

A paper now in page proofs has been modified by the copy editor to have figure tags (e.g. 'A', 'B', ...) in bold, while the rest of the label is normal. For example "A matches > 30", "B matches > 60". I cannot figure out how to get facet_wrap() labeller to bold only part of the label string. I have not figured out how to get labeller=label_parsed to accept my bold string with >= in it.

stefan
  • 90,330
  • 6
  • 25
  • 51

2 Answers2

0

As you provided no minimal reproducible example I don't know what's the issue with your code.

But here is a minimal reprex showing how you could achieve your desired result. Note the use of the ~ after bold

dat <- data.frame(
  x = 1,
  y = 1,
  facet = c("bold(A)~matches > 30", "bold(B)~matches >= 60")
)

library(ggplot2)

ggplot(dat, aes(x, y)) +
  geom_point() +
  facet_wrap(~facet, labeller = label_parsed)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you very much. Could you show me an example using an as_labeller() object? When I do the something very similar to what you have, (a named vector with the 4 strings I want with bold() and >=), and then use `facet_wrap(variable,labeller=my_labeller)`, I get the message: `Error in parse(text = as.character(values)) : :1:19: unexpected symbol 1: bold(A)~alignment length` – Bill Pearson Nov 09 '22 at 18:00
  • The best I could do would be to copy and paste the example from `?as_labeller`. (; Actually I have never used this in practice. – stefan Nov 09 '22 at 18:03
  • How did you learn to only put the '~' after bold(A)? – Bill Pearson Nov 09 '22 at 18:06
  • A bit of trial and error. Struggled with `?plotmath` myself in the beginning. But I learned that the result must be a valid expression and that most issues could be fixed by using either a `*` or a `~`. – stefan Nov 09 '22 at 18:15
  • 1
    Hm. I'm pretty sure it does. But without a minimal reproducible example of your approach it's hard to come up with a working solution. You could e.g. take my answer as starting point to create a minimal reprex suited to your real case. – stefan Nov 09 '22 at 20:28
0

I believe I have found a bug in plotmath/as_labeller. The script below shows the problem:

library(ggplot2)

f_vals = c(0.5, 0.8, 1.2, 1.5)
labs = c("A","B","C","D")

all_data=NULL

for (ix in 1:length(f_vals)) {
    dat <- data.frame(x=rnorm(20,mean=0,sd=f_vals[ix]),
        facet=f_vals[ix])

    all_data <- rbind(all_data,dat)
}
## this line fails with "sd:"
f_labs = sprintf("bold(%s)~~~the~sd ==~%.2f",labs, f_vals)
names(f_labs) = f_vals

facet_labeller = as_labeller(f_labs, label_parsed)

pA = ggplot(all_data, aes(x, x)) +
  geom_point() +
  theme(strip.text=element_text(hjust=0)) +
  facet_wrap(~facet, labeller = facet_labeller)

pdf(file="test_bold3.pdf",width=8.0, height=6.0)
pA

As written, this code works as expected. But if there is a colon (:) in the label string, e.g.

f_labs = sprintf("bold(%s)~~~the~sd: ==~%.2f",labs, f_vals)

The script fails with the message:

Error in parse(text = as.character(values)) : 
  <text>:1:19: unexpected '=='
1: bold(A)~~~the~sd: ==
                      ^
Calls: <Anonymous> ... labeller -> default -> lapply -> FUN -> FUN -> parse
Execution halted

I was unable to find a combination of spaces and '~' that allowed a ':' in the label string.