ggplot2 主题中的 xlab

问题描述 投票:0回答:2

我正在制作许多具有相同 xlab 的图表。我想知道我们可以在主题中使用 xlab 这样的吗:

theme_mine1 <- opts(
      panel.grid.major = theme_blank()
    , panel.grid.minor = theme_blank()
    , panel.background = theme_blank()
    , axis.line        = theme_blank()
    , xlab             = expression(paste("My (xlab ", m^{-2}, ")"))
    )

当我使用这个主题时,它不会给出任何错误或警告,但它不会更改 xlab。接下来我可以尝试什么?

r ggplot2
2个回答
9
投票

您可以使用

opts(labels=list(x='xlabelhere'),...)

关于我如何发现这一点的说明(因为我以前不知道,并且认为它非常有用):

有一个很棒的 stackoverflow 问题,关于查找可以输入到

opts
这里的内容。

总而言之,它引用了 ggplot2 wiki 的

opts
。 它 also 表示您可以使用
plot_theme(p)
查看当前应用于绘图的所有选项
p

在您的情况下,ggplot2

opts
链接没有为x标签产生任何结果,但是从您之前的问题中对
plot_theme(p)
执行
p
,人们可以看到:

> names(plot_theme(p))
 [1] "labels"             "axis.line"          "axis.text.x"       
 [4] "axis.text.y"        "axis.ticks"         "axis.title.x"      
 [7] "axis.title.y"       "axis.ticks.length"  "axis.ticks.margin" 
[10] "legend.background"  "legend.key"         "legend.key.size"   
[13] "legend.key.height"  "legend.key.width"   "legend.text"       
[16] "legend.text.align"  "legend.title"       "legend.title.align"
[19] "legend.position"    "legend.direction"   "legend.box"        
[22] "panel.background"   "panel.border"       "panel.grid.major"  
[25] "panel.grid.minor"   "panel.margin"       "strip.background"  
[28] "strip.text.x"       "strip.text.y"       "plot.background"   
[31] "plot.title"         "plot.margin"       

就您的目的而言,

labels
看起来非常有前途!

然后我尝试了:

> plot_theme(p)$labels
$x
[1] "x"

$y
[1] "y/n"

得分!这让我足够继续下去:

theme_mine1 <- opts(
                ....,
                labels=list(x='my xlabel! booya!'))

这有效!


3
投票

xlab
不是
opts()
的参数,而是一个单独的函数,请尝试这个,

qplot(1, 1) + list(theme_mine1, xlab("x label"))
© www.soinside.com 2019 - 2024. All rights reserved.