首页 > 解决方案 > ggplot的方面标题中的部分斜体

问题描述

我想知道是否有任何方法可以重命名构面标题,以便它们包含部分斜体和部分非斜体。

这是一些玩具数据

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

在此处输入图像描述

有没有办法让分面标题读取“ p = 0.001”、“ p = 0.01”等,而不是“p = 0.001”,即部分斜体和部分非斜体?

标签: rggplot2

解决方案


推荐阅读