使用 textGrob() 和 ggplot2 在 grob 中调整字体大小
Adjusting font size within a grob using textGrob() and ggplot2
我正在尝试增加作为 grob 添加的第二个轴标题的字体大小(出于将变得明显的原因)。这是一些要绘制的玩具数据
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 odds ratios, spanning the 95% CI centered around what we got (OR=2.9)
alpha <- c(.001, .01, .05)
# 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"))))
Now for the figure
library(grid)
library(gtable)
p2 <- 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)) +
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"))
p2
现在将第二个轴标题添加为 grob。
g <- ggplotGrob(p2)
rect <- grobTree(rectGrob(gp = gpar(fill = "white", col = "white")),
textGrob(expression(bold("Significance Level")), rot = -90, gp = gpar(col = "black")))
g <- gtable_add_cols(x = g, widths = g$widths[6], pos = 6) # add a column g$widths[6] wide to the right of horizontal position 6
g <- gtable_add_grob(x = g, grobs = rect, l=7, t=7, b=11) # now add the rect grob at the new column position to the right of position 6 (i.e. left-most position y, or l= 7, spanning the whole graph)
grid.newpage()
grid.draw(g)
都很好。但是 如何在 textGrob()
内增加第二个轴的字体大小?
我已尝试 fontsize =
、cex =
、face =
无济于事,textGrob()
文档未提及字体大小。
字体大小通过 gpar()
中的 fontsize
参数设置。
g <- ggplotGrob(p2)
rect <- grobTree(
rectGrob(gp = gpar(fill = "white", col = "white")),
textGrob(
expression(bold("Significance Level")), rot = -90,
gp = gpar(col = "black", fontsize = 20)
)
)
g <- gtable_add_cols(x = g, widths = g$widths[6], pos = 6) # add a column g$widths[6] wide to the right of horizontal position 6
g <- gtable_add_grob(x = g, grobs = rect, l=7, t=7, b=11) # now add the rect grob at the new column position to the right of position 6 (i.e. left-most position y, or l= 7, spanning the whole graph)
grid.newpage()
grid.draw(g)
我正在尝试增加作为 grob 添加的第二个轴标题的字体大小(出于将变得明显的原因)。这是一些要绘制的玩具数据
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 odds ratios, spanning the 95% CI centered around what we got (OR=2.9)
alpha <- c(.001, .01, .05)
# 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"))))
Now for the figure
library(grid)
library(gtable)
p2 <- 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)) +
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"))
p2
现在将第二个轴标题添加为 grob。
g <- ggplotGrob(p2)
rect <- grobTree(rectGrob(gp = gpar(fill = "white", col = "white")),
textGrob(expression(bold("Significance Level")), rot = -90, gp = gpar(col = "black")))
g <- gtable_add_cols(x = g, widths = g$widths[6], pos = 6) # add a column g$widths[6] wide to the right of horizontal position 6
g <- gtable_add_grob(x = g, grobs = rect, l=7, t=7, b=11) # now add the rect grob at the new column position to the right of position 6 (i.e. left-most position y, or l= 7, spanning the whole graph)
grid.newpage()
grid.draw(g)
都很好。但是 如何在 textGrob()
内增加第二个轴的字体大小?
我已尝试 fontsize =
、cex =
、face =
无济于事,textGrob()
文档未提及字体大小。
字体大小通过 gpar()
中的 fontsize
参数设置。
g <- ggplotGrob(p2)
rect <- grobTree(
rectGrob(gp = gpar(fill = "white", col = "white")),
textGrob(
expression(bold("Significance Level")), rot = -90,
gp = gpar(col = "black", fontsize = 20)
)
)
g <- gtable_add_cols(x = g, widths = g$widths[6], pos = 6) # add a column g$widths[6] wide to the right of horizontal position 6
g <- gtable_add_grob(x = g, grobs = rect, l=7, t=7, b=11) # now add the rect grob at the new column position to the right of position 6 (i.e. left-most position y, or l= 7, spanning the whole graph)
grid.newpage()
grid.draw(g)