2

Continuation to this question.

The labels in facet_wrap() contains numbers with different decimal places. I used glue R package. The codes and the associated figures are given below.

data <- data.frame(N = runif(30, 200, 600),
                   powers = runif(30, 0.03, 0.06),
                   low = runif(30, 0.03, 0.04),
                   upp = runif(30, 0.04, 0.05),
                   q = rep(c(0.02,0.04,0.06,0.08,0.10,0.12), 5))

N = data[,1]
p = data[,2]
q = data[,3]
low = data[,4]
upp = data[,6]
powers = data[,5]

ggplot(data,aes("x" = N, "y" = powers))+
  geom_point() + geom_line() +
  facet_wrap(~glue('phi[1]*" = {q}"'), nrow = 2, labeller = label_parsed)+
  labs("y" = "Powers")

Figure 1

How do I make sure that the labels wrote $\phi_1 = 0.10$ instead of $\phi_1 = 0.1$?

RRMT
  • 137
  • 5
  • 1
    It's hard to know if answers are correct if we cannot reproduce the problem. Can you share part of your data? (or similar data that reproduce the problem?) – Maël Aug 25 '22 at 10:07
  • I have added randomly produced data. The plots will be different but it is sufficient. – RRMT Aug 25 '22 at 13:54
  • Can you check your code again, there is no powers? – Maël Aug 25 '22 at 13:58
  • 1
    Sorry, I have fixed it. Unfortunately, I can give the data because it is long. Your answer has fixed it, after I tweaked it just tiny bit. – RRMT Aug 25 '22 at 14:17

1 Answers1

4

With format and nsmall:

... +
facet_wrap(~ glue('phi[1]*" = {format(round(q, 2), nsmall = 2)}"') +
...
Maël
  • 45,206
  • 3
  • 29
  • 67
  • 1
    Unfortunately, both did not work. but I tweaked it. Second method digits = 2, worked. – RRMT Aug 25 '22 at 14:03