簡體   English   中英

如何在 ggplot2 上調整輔助 Y 軸的間距或邊距?

[英]How to adjust spacing or margin for secondary Y axis on ggplot2?

我正在使用以下代碼在 R 中處理 ggplot:

s=40
ggplot(data = NULL) +
  geom_line(data = stk, aes(x = date, y = price)) +
  geom_line(data = gdp, aes(x = year, y = gdp1*s)) +
  scale_y_continuous("Index of Total Stock Price",
                     sec.axis = sec_axis(~. /s, name = "Real GDP (trillions of USD in 2012)"),
                     limits = c(0, 100)) +
  scale_x_date(breaks = seq(from = as.Date("1900-01-01"),
                            to = as.Date("1945-01-01"),
                            by = "5 years"),
               labels=date_format("%Y")) +
  xlab(element_blank())

而這個 output:

在此處輸入圖像描述

問題是輔助軸上的文本和刻度看起來非常狹窄。 對於兩個 y 軸標簽,如何使刻度和軸 label 之間的空間相同? 謝謝。

編輯:

這是可重現的數據:

set.seed(1000)
df1 <- data.frame(y1=rnorm(100, 0, 1),
                  x=seq(1, 100, 1))
df2 <- data.frame(y2=rnorm(100, 0, 1),
                  x=seq(1, 100, 1))
ggplot(data = NULL) +
  geom_line(data = df1, aes(x = x, y = y1)) +
  geom_line(data = df2, aes(x = x, y = y2*2)) +
  scale_y_continuous("Label of Primary Y Axis",
                     sec.axis = sec_axis(~. *2, name = "Label of Secondary Y Axis"))

在此處輸入圖像描述

我有同樣的問題,使用第二部分,

theme(axis.title.y.right = element_text(margin = margin(t = 0, r = 0, b = 0, l = 10))

為我工作。

暫無
暫無

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

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