简体   繁体   中英

Adding decimal places for glue command in ggplot

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")

图1

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

With format and nsmall :

... +
facet_wrap(~ glue('phi[1]*" = {format(round(q, 2), nsmall = 2)}"') +
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM