如何在 R 中自定义 Facet_Grid 字体标签
How to Customize Facet_Grid Font Label in R
我有这个 ggplot
和 facet_grid
功能:
set.seed(1)
df <- data.frame(xx = 1:10, x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10), x4 = rnorm(10), x5 = rnorm(10), x6 = rnorm(10), x7 = rnorm(10), x8 = rnorm(10), x9 = rnorm(10), x10 = rnorm(10), x11 = rnorm(10), x12 = rnorm(10))
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
pivot_longer(-xx) %>%
mutate(id = as.numeric(gsub("x", "", name))) %>%
arrange(id, xx) %>%
select(-id) %>%
mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
phi = rep(rep(list(c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6)), each = 10), 4)) %>%
mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
phi = factor(phi, levels = phi, labels = gsub("c", "", paste("\U03D5 =", phi)))) %>%
ggplot(aes(x = xx, y = value)) +
geom_line() +
geom_point() +
scale_y_continuous(expand = c(0.0, 0.00)) +
labs(x = "Time", y = "Series") +
facet_grid(sd ~ phi, scales = "free_y") +
theme_bw()
作为 提供。
我想要的
我想增加(或自定义)右侧的标签 sd = c(sd = 1, sd = 3, sd = 5, sd = 10) 和顶部的标签 phi = c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6)) 的图。还有,如何让它们变粗。
最简单的方法是:
previous_code_blocks+
theme(strip.text.x = element_text(size = 10, face = "bold"),
strip.text.y = element_text(size = 10, face = "bold"))
您可以在此处使用常用的 element_text() 参数进行自定义,更改字体系列和其他类似内容,hjust、vjust 等。
如果您希望在未来对贴标机进行更多控制,或者需要更高级的定制,您可以在以下位置找到文档:https://ggplot2.tidyverse.org/reference/labeller.html
我有这个 ggplot
和 facet_grid
功能:
set.seed(1)
df <- data.frame(xx = 1:10, x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10), x4 = rnorm(10), x5 = rnorm(10), x6 = rnorm(10), x7 = rnorm(10), x8 = rnorm(10), x9 = rnorm(10), x10 = rnorm(10), x11 = rnorm(10), x12 = rnorm(10))
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
pivot_longer(-xx) %>%
mutate(id = as.numeric(gsub("x", "", name))) %>%
arrange(id, xx) %>%
select(-id) %>%
mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
phi = rep(rep(list(c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6)), each = 10), 4)) %>%
mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
phi = factor(phi, levels = phi, labels = gsub("c", "", paste("\U03D5 =", phi)))) %>%
ggplot(aes(x = xx, y = value)) +
geom_line() +
geom_point() +
scale_y_continuous(expand = c(0.0, 0.00)) +
labs(x = "Time", y = "Series") +
facet_grid(sd ~ phi, scales = "free_y") +
theme_bw()
作为
我想要的
我想增加(或自定义)右侧的标签 sd = c(sd = 1, sd = 3, sd = 5, sd = 10) 和顶部的标签 phi = c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6)) 的图。还有,如何让它们变粗。
最简单的方法是:
previous_code_blocks+
theme(strip.text.x = element_text(size = 10, face = "bold"),
strip.text.y = element_text(size = 10, face = "bold"))
您可以在此处使用常用的 element_text() 参数进行自定义,更改字体系列和其他类似内容,hjust、vjust 等。
如果您希望在未来对贴标机进行更多控制,或者需要更高级的定制,您可以在以下位置找到文档:https://ggplot2.tidyverse.org/reference/labeller.html