ggplot2主题:axis.text不从文本继承吗? [英] ggplot2 theme: axis.text not inheriting from text?

查看:50
本文介绍了ggplot2主题:axis.text不从文本继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题元素 axis.text 的当前文档说:

axis.text

axis.text

沿轴打勾标签(element_text;继承自文本)

tick labels along axes (element_text; inherits from text)

但似乎继承无效.

此代码给出了下面的图,其中轴文本为灰色.

This code gives the plot below, with axis text in grey.

library(ggplot2)
ggplot(data.frame(x=1:10, y=1:10), aes(x, y)) +
  geom_point(color='red') +
  theme(rect = element_rect(fill = 'black'),
        line = element_line(color = 'white'),
        text = element_text(color = 'blue'),
        panel.background = element_blank())

显式设置 axis.text 可以,但是我希望代码片段1已经产生了此结果

Setting axis.text explicitly works, but I was expecting code snippet 1 to already produce this result

ggplot(data.frame(x=1:10, y=1:10), aes(x, y)) +
  geom_point(color='red') +
  theme(rect = element_rect(fill = 'black'),
        line = element_line(color = 'white'),
        text = element_text(color = 'blue'),
        
        # *** setting this explictly ***
        axis.text = element_text(color = 'blue'),
        
        panel.background = element_blank())

我从一个新的R会话开始,使用这个 sessionInfo()

I'm starting from a fresh R session, with this sessionInfo()

R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.0

loaded via a namespace (and not attached):
 [1] labeling_0.3     colorspace_1.2-6 scales_0.4.1     assertthat_0.1   lazyeval_0.2.0  
 [6] plyr_1.8.4       tools_3.3.1      gtable_0.2.0     tibble_1.1       Rcpp_0.12.6     
[11] grid_3.3.1       munsell_0.4.3   

如何指定高级主题元素,并通过继承使这些设置级联?我需要以某种方式清除"默认主题吗?

How can I specify high-level theme elements and let those settings cascade down via inheritance? Do I need to somehow 'clear' the default theme?

推荐答案

不同之处在于,在第一个示例中,对 theme()的调用导致了一个不完整"的 theme 对象.

The difference is that your call to theme() in the first example results in an "incomplete" theme object.

考虑:

attr(theme(rect = element_rect(fill = 'black'),
           line = element_line(colour = 'white'),
           text = element_text(colour = 'blue'),
           panel.background = element_blank()), "complete")

这应该返回 FALSE .我的理解是,主题不完整时对 theme()的调用不会具有所有继承.像 theme_grey()这样的主题是完整的,因此继承有效.我不是100%积极的人,但是文档中的这一行似乎暗示了这一点.

This should return FALSE. My understanding is that a call to theme() when the theme is incomplete does not have all of the inheritance. A theme like theme_grey() is complete and so inheritance works. I'm not 100% positive however, but this line in the documentation seems to suggest it.

通过调用完整主题函数返回的对象现在是主题元素及其属性的嵌套列表,使支持属性继承的新主题系统.

The object returned by a call to a complete theme function is now a nested list of theme elements and their properties, which enables the new theming system to support inheritance of properties.

查看此页面,特别是称为的部分"完整和不完整的主题对象" .它在那里讨论了有关颜色的相同问题,并向您展示了如何在继承有效的地方创建自己的[完整]自定义主题.

Look at this page, specifically the section called "Complete and incomplete theme objects". It discusses the same issues with color there, and shows you how to create your own [complete] custom theme where inheritance works.

这篇关于ggplot2主题:axis.text不从文本继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆