簡體   English   中英

方面標題 alignment 在 ggplot2 中使用 facet_wrap()?

[英]Facet title alignment using facet_wrap() in ggplot2?

我正在制作一個涉及面的 plot,我正在嘗試修復面/條帶標題的 alignment。 現在它似乎與面板左對齊,將其放置在列底部和軸刻度之間的間隙上。 我最好讓它與圖表的底部對齊,或者完全左對齊並將列的底部移動到更靠近 y 軸 label. Reprex 下面。

library(tidyverse)

tibble(ToothGrowth) %>% 
  mutate(dose = as_factor(dose),
         supp = as_factor(supp)) %>% 
  group_by(supp, dose) %>% 
  summarise(x = median(len)) %>% 
  ggplot(aes(y = supp, x = x)) +
  geom_col(fill = "grey55") +
  facet_wrap(~dose, ncol = 1) +
  labs(title = "Growing Teeth are less interesting than Irises") +
  theme_minimal() +
  theme(strip.placement = "inside",
        strip.text = element_text(hjust = 0),
        strip.background = element_blank(),
        panel.background = element_rect(fill = "grey95",
                                        color = NA))

reprex package (v0.3.0) 創建於 2020-05-25

這是你想要的?

本質上,我們將分面條放在面板的左側,並設置(未記錄的)主題元素strip.text.y.left

我可以想象您甚至希望將條形文本超出 y 軸標題,但恐怕如果不深入研究圖表的網格/gtables,這是不可能的。

library(tidyverse)

tibble(ToothGrowth) %>% 
  mutate(dose = as_factor(dose),
         supp = as_factor(supp)) %>% 
  group_by(supp, dose) %>% 
  summarise(x = median(len)) %>% 
  ggplot(aes(y = supp, x = x)) +
  geom_col(fill = "grey55") +
  facet_wrap(~dose, ncol = 1, strip.position = "left") +
  labs(title = "Growing Teeth are less interesting than Irises") +
  theme_minimal() +
  theme(strip.placement = "outside",
        strip.text.y.left = element_text(angle = 0, vjust = 1),
        strip.background = element_blank(),
        panel.background = element_rect(fill = "grey95",
                                        color = NA))

代表 package (v0.3.0) 於 2020 年 5 月 25 日創建

這是使用expand = c(0,0)和使用hjust = -0.01輕推的@teunbrand 的替代方法:

plot.data <- tibble(ToothGrowth) %>% 
  mutate(dose = as_factor(dose),
         supp = as_factor(supp)) %>% 
  group_by(supp, dose) %>% 
  summarise(x = median(len)) %>%
ggplot(aes(y = supp, x = x)) +
  geom_col(fill = "grey55") +
  scale_x_continuous(expand = c(0, 0)) +
  facet_wrap(~dose, ncol = 1) +
  labs(title = "Growing Teeth are less interesting than Irises") +
  theme_minimal() +
  theme(strip.text.x = element_text(hjust = -0.01),
        panel.background = element_rect(fill = "grey95",
                                        color = NA))

在此處輸入圖像描述

一個更好的選擇是設置hjust=0, margin=margin(l=0)

plot.data <- tibble(ToothGrowth) %>% 
    mutate(dose = forcats::as_factor(dose),
           supp = forcats::as_factor(supp)) %>% 
    group_by(supp, dose) %>% 
    summarise(x = median(len)) %>%
    ggplot(aes(y = supp, x = x)) +
    geom_col(fill = "grey55") +
    scale_x_continuous(expand = c(0, 0)) +
    facet_wrap(~dose, ncol = 1) +
    labs(title = "Growing Teeth are less interesting than Irises") +
    theme_minimal() +
    theme(strip.text.x = element_text(hjust = 0, margin=margin(l=0)),
          panel.background = element_rect(fill = "grey95",
                                          color = NA))

在此處輸入圖像描述

原因是 hjust 是 plot 區域的百分比,而您要補償的偏移量是絕對單位的邊距(我認為是 pts)。 因此,所需的 hjust 偏移量特定於 plot 寬度。 如果您將圖形拖得更寬或更窄,則在使用 hjust 時條帶文本的位置將相對於條形“移動”,而在使用“邊距”時它會保持一致。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM