简体   繁体   English

ggplot2中的主题中的xlab

[英]xlab in theme in ggplot2

I'm making many graphs which have same xlab. 我正在制作许多具有相同xlab的图形。 I wonder can we use xlab in theme like this: 我想知道我们是否可以在主题中使用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}, ")"))
    )

When I use this theme it doesn't give any error or warning but it does not change the xlab. 当我使用此主题时,它不会给出任何错误或警告,但不会更改xlab。 Any suggestion will be highly appreciated. 任何建议将不胜感激。 Thanks 谢谢

You can use opts(labels=list(x='xlabelhere'),...) . 您可以使用opts(labels=list(x='xlabelhere'),...)

A note on how I found this out (because I didn't know it before, and think it's very useful): 关于如何发现此问题的注释(因为我以前不知道,因此认为它非常有用):

There is a fantastic stackoverflow question on finding what can be fed in to opts here . 目前在寻找什么可以送入一个梦幻般的计算器问题, opts 在这里

In summary it references the ggplot2 wiki for opts . 总之,它引用ggplot2 wiki来获取opts It also says you can use plot_theme(p) to see all options currently applied to plot p . 它还说,你可以使用plot_theme(p)看到当前应用于绘制所有选项p

In your case the ggplot2 opts link didn't yield any results for the x label, but doing plot_theme(p) on p from your previous questions, one could see: 在您的情况下,ggplot2 opts链接对于x标签没有产生任何结果,但是从您先前的问题中对p进行plot_theme(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"       

For your purposes, labels looks very promising! 为了您的目的, labels看起来很有希望!

So then I tried: 所以我尝试了:

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

$y
[1] "y/n"

Score! 得分! This gives me enough to go on: 这给了我足够的继续:

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

Which works! 哪个有效!

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

qplot(1, 1) + list(theme_mine1, xlab("x label"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM