簡體   English   中英

使用非默認軸位置時,如何增加軸標題與繪圖的距離?

[英]How to increase the distance of an axis title from the plot when using non-default axis positions?

我想增加小平面條和軸標題之間的間距。

我有一個使用facet_grid ,軸標題位於非默認位置:頂部和右側。

axis.titlemargin參數axis.title沒有影響: theme(axis.title = element_text(margin = margin(20, 20, 20, 20)))不執行任何操作。 我試過通過scale而不是theme來抑制axis.tickaxis.text ,但這沒什么區別。

這是一個可重現的示例:

example.data <- data.frame(x.var = rep(-2:2, 5),
                           y.var = rep(-2:2, each=5),
                           boolean.var = as.logical(sample(1:1000, 25) %% 2))

library(ggplot2)
library(tidyr)
example.data %>% ggplot(aes(fill = boolean.var)) +
    geom_rect(xmin = -1, xmax = 1, ymin = -1, ymax = 1) +
    scale_x_continuous(name = "(X Title)", position = "top", limits = c(-0.5,0.5)) +
    scale_y_continuous(name = "(Y Title)", position = "right", limits = c(-0.5,0.5)) +
    scale_fill_discrete(guide = FALSE) +
    facet_grid(y.var ~ x.var) +
    theme(panel.margin=unit(0.25 , "lines"),
          axis.title = element_text(size = 24, margin = margin(20, 20, 20, 20)),
          axis.ticks = element_blank(),
          axis.text = element_blank(),
          axis.title = element_text(margin = margin(20, 20, 20, 20)))

這是輸出:

在此處輸入圖片說明

在這種情況下,必須使用參數axis.title.x.topaxis.title.y.right

單獨指定axis.title.xaxis.title.y ,當軸標題位於默認位置(向下,向左)時,它們仍然有效。

這是完整的示例,帶有正確的間距:

example.data <- data.frame(x.var = rep(-2:2, 5),
                           y.var = rep(-2:2, each=5),
                           boolean.var = as.logical(sample(1:1000, 25) %% 2))

library(ggplot2)
library(tidyr)
example.data %>% ggplot(aes(fill = boolean.var)) +
    geom_rect(xmin = -1, xmax = 1, ymin = -1, ymax = 1) +
    scale_x_continuous(name = "(X Title)", position = "top", limits = c(-0.5,0.5)) +
    scale_y_continuous(name = "(Y Title)", position = "right", limits = c(-0.5,0.5)) +
    scale_fill_discrete(guide = FALSE) +
    facet_grid(y.var ~ x.var) +
    theme(panel.margin=unit(0.25 , "lines"),
          axis.title = element_text(size = 24, margin = margin(20, 20, 20, 20)),
          axis.ticks = element_blank(),
          axis.text = element_blank(),
          axis.title.x.top = element_text(margin = margin(1, 0, 15, 0)),
          axis.title.y.right = element_text(margin = margin(0, 1, 0, 15)))

給出以下輸出:

在此處輸入圖片說明

暫無
暫無

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

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