【问题标题】:How to Customize Facet_Grid Font Label in R如何在 R 中自定义 Facet_Grid 字体标签
【发布时间】:2021-12-27 12:38:36
【问题描述】:

我有这个ggplotfacet_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()

satisfactory answer for this question 的形式提供。

我想要什么

我想增加(或自定义)右侧的标签 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)) 的情节。还有,如何让它们加粗。

【问题讨论】:

    标签: r ggplot2 facet-grid


    【解决方案1】:

    最简单的方法是:

        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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2011-09-06
      • 2013-02-24
      • 1970-01-01
      • 2017-04-17
      • 2011-07-17
      相关资源
      最近更新 更多