2

I am wondering if there is any way to rename facet titles so that they contain partial italics and partial non-italics.

Here is some toy data

library(Hmisc)
library(dplyr)

# Plot power vs. n for various odds ratios 
n  <- seq(10, 1000, by=10) # candidate sample sizes
OR <- as.numeric(sort(c(seq(1/0.90,1/0.13,length.out = 9),2.9))) # candidate ORs
alpha <- c(.001, .01, .05) # alpha significance levels

# put all of these into a dataset and calculate power
powerDF <- data.frame(expand.grid(OR, n, alpha)) %>% 
           rename(OR = Var1, num = Var2, alph = Var3) %>%
           arrange(OR) %>%
           mutate(power = as.numeric(bpower(p1=.29, odds.ratio=OR, n=num, alpha = alph))) %>%
           transform(OR = factor(format(round(OR,2),nsmall=2)),
                     alph = factor(ifelse(alph == 0.001, "p=0.001",
                                          ifelse(alph == 0.01, "p=0.01", "p=0.05"))))

pPower <- ggplot(powerDF, aes(x = num, y = power, colour = factor(OR))) + 
                geom_line() +
                facet_grid(factor(alph)~.) +
                labs(x = "sample size") +
                scale_colour_discrete(name = "Odds Ratio") +
                scale_x_continuous(breaks = seq(0,1000,100)) +
                scale_y_continuous(breaks = seq(0,1,.1), sec.axis = sec_axis(trans=I, breaks=NULL, name="Significance Level")) + # this is the second axis label
                theme_light() +
                theme(axis.title.x = element_text(size = 12, face = "bold"),
                      axis.title.y = element_text(size = 12, face = "bold"),
                      axis.text = element_text(size = 11),
                      panel.grid.minor = element_blank(),
                      panel.grid.major.y = element_line(colour = "gray95"),
                      panel.grid.major.x = element_line(colour = "gray95"),
                      strip.text = element_text(colour = 'black', face = 'bold', size = 12),

                      legend.text = element_text(size = 12),
                      legend.title = element_text(size = 12, face = "bold"))

pPower

enter image description here

Is there any way to get the facet headings to read "p=0.001", "p=0.01" etc, instead of "p=0.001", i.e. to get partial italics and partial non-italics?

llewmills
  • 2,959
  • 3
  • 31
  • 58
  • https://stackoverflow.com/questions/16490331/combining-new-lines-and-italics-in-facet-labels-with-ggplot2 seems to give an answer. – user20650 Oct 15 '19 at 00:20
  • 4
    i.e use `levels(powerDF$alph) <- c("italic('p=')*0.001", "italic('p=')*0.01", "italic('p=')*0.05")` and ` facet_grid(alph~., labeller=label_parsed) ` – user20650 Oct 15 '19 at 00:21
  • Fantastic @user20650! Put it in an answer, however small, and I'll accept. – llewmills Oct 15 '19 at 01:03
  • glad it worked llewmills.Ive closed this as a dup of the other question as it just uses the solution from there with minor tweaks to your problem. I'll reopen if you disagree and wish to post an answer yourself. – user20650 Oct 15 '19 at 08:52
  • Happy for you to close, though I am very glad you answered this because I doubt I would have been able to extract the all-important labeller function from the sea of surrounding code in that post. Might just answer this myself. – llewmills Oct 16 '19 at 06:37
  • llewmills; Ive reopened so you can put an answer down. – user20650 Oct 16 '19 at 08:12

0 Answers0