簡體   English   中英

在ggplot2中為主題添加一個參數

[英]Add an argument to theme in ggplot2

我有一個以ggplot2為主題的全局變量:

cPlotOpts <- opts (axis.text.x = theme_text (size=10, colour="grey50"), axis.text.y = theme_text (…

我想稍后在代碼中添加一個參數,但不修改那些已設置的參數,這樣就可以了

axis.text.x = theme_text (size=10, colour="grey50", angle=90)

如何將這一個參數( angle )添加到已定義的theme_text而不必顯式重復sizecolour的設置?

[在第一個答案后編輯,以便更清晰。]

我認為最簡單的方法是使用一個函數。

cPlotOpts <- function(size = 10, colour = "grey50", ...) {
  opts(axis.text.x = theme_text (size=size, colour=colour, ...))
}

然后在以后添加一個參數,簡單地說:

cPlotOpts(angle=90)

產量:

cPlotOpts(angle=90)
$axis.text.x
theme_text(colour = colour, size = size, angle = 90)

attr(,"class")
[1] "options"

如果您不想編輯它,只需使用cPlotOpts() 是這樣的嗎?

暫無
暫無

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

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