2

尝试将主题切换theme_bw()为不起作用,theme_grey()似乎是优先考虑的。任何想法为什么?

ggplot(data=mpg,aes(y=year,x=cyl)) + geom_point() + theme_bw()

在此处输入图像描述

编辑

正如评论中所建议的,这在干净的 R 会话中运行时有效。但是当我在ggtern加载的会话中实现代码时,问题就出现了。

library(ggplot2)
#Warning message:
#package ‘ggplot2’ was built under R version 3.2.5 
ggplot(data=mpg,aes(y=year,x=cyl)) + geom_point() + theme_bw()

# sessionInfo()
# R version 3.2.2 (2015-08-14)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 8 x64 (build 9200)
# 
# locale:
#   [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
# [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.1252    
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] ggplot2_2.1.0
# 
# loaded via a namespace (and not attached):
#   [1] labeling_0.3     colorspace_1.2-6 scales_0.4.0     plyr_1.8.3       tools_3.2.2      gtable_0.1.2    
# [7] Rcpp_0.12.2      grid_3.2.2       munsell_0.4.2 

在此处输入图像描述

library(ggtern)
#Loading required package: ggplot2

#Attaching package: ‘ggtern’

#The following objects are masked from ‘package:ggplot2’:

    #aes, calc_element, ggplot, ggplot_build, ggplot_gtable, ggplotGrob, ggsave, is.ggplot, layer_data,
    #layer_grob, layer_scales, theme, theme_bw, theme_classic, theme_dark, theme_get, theme_gray,
    #theme_light, theme_linedraw, theme_minimal, theme_set, theme_void

#Warning messages:
#1: package ‘ggtern’ was built under R version 3.2.5 
#2: package ‘ggplot2’ was built under R version 3.2.5 
ggplot(data=mpg,aes(y=year,x=cyl)) + geom_point() + theme_bw()

# sessionInfo()
# R version 3.2.2 (2015-08-14)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 8 x64 (build 9200)
# 
# locale:
#   [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
# [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.1252    
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] ggtern_2.1.1  ggplot2_2.1.0
# 
# loaded via a namespace (and not attached):
#   [1] Rcpp_0.12.2         lattice_0.20-33     MASS_7.3-45         grid_3.2.2          plyr_1.8.3         
# [6] bayesm_3.0-2        gtable_0.1.2        magrittr_1.5        scales_0.4.0        stringi_1.0-1      
# [11] compositions_1.40-1 robustbase_0.92-5   latex2exp_0.4.0     boot_1.3-17         labeling_0.3       
# [16] proto_0.3-10        tools_3.2.2         stringr_1.0.0       energy_1.6.2        DEoptimR_1.0-4     
# [21] munsell_0.4.2       colorspace_1.2-6    tensorA_0.36        gridExtra_2.0.0    

在此处输入图像描述

看起来 ggtern 掩盖了 ggplot2 的许多主题。

4

3 回答 3

3

ggtern掩码theme_bw(),这意味着ggtern有自己的版本theme_bw()覆盖该ggplot2版本。奇怪的是,ggtern的版本theme_bw()看起来像theme_grey(),所以这可能是一个错误。

如果要保持ggtern加载,另一种选择是强制执行以下ggplot2版本theme_bw()

ggplot(data=mpg,aes(y=year,x=cyl)) + geom_point() + ggplot2::theme_bw()

不幸的是,您不能只在加载ggplot2后加载ggtern(以避免屏蔽问题),因为ggtern加载ggplot2.

要查看每个版本的代码差异,请theme_bw()在控制台中键入以下内容:

ggplot2::theme_bw

ggtern::theme_bw

更新:上述主题之间的冲突发生在2.1.2ggplot2及更早版本中,但冲突不再发生在 2.1.3 版本中,因此您需要做的就是更新到最新版本的. 有关详细信息,请参阅@NicolasHamilton 的答案。ggternggternggtern

于 2016-07-18T18:38:20.567 回答
3

ggtern掩盖ggplot2中的每个默认主题,这是因为在编写此软件时,已经创建了大约 60 个新的主题元素,这些元素的存在是为了ggtern正确渲染。有关新主题元素的完整列表,请运行以下命令:

library(ggtern)
?theme_elements

如上所述,我注意到一些冲突,正如你所指出的,由于ggtern还修改了一些基本主题元素,并且由于关于 version 2.1.2 - 2.1.3,我已经完全重新设计了主题,因此ggtern不再修改任何基本主题元素——希望这种烦人的行为现在已经永远消失了!

我正在制作有关该软件包的出版物,并且已经改进了许多长期的烦恼,因此请2.1.4从我的 ( Bitbucket Repository ) 下载并安装最新版本 ( )。令人尴尬的是,即使是2.1.3CRAN 上的最后一个版本,当我在一个grid.arrange类型的情况下运行两个绘图时,我发现了一个非常重要的错误。这是剪贴蒙版的问题,并且已经解决in 2.1.4但已发送给 CRAN,这是我计划立即做的事情。

无论如何,要回答你的问题,请升级你的版本,你正在运行版本2.1.0,这是在一个巨大的修订后不久发布的版本ggplot2——事实上,不久之前,以前的版本被严重破坏,几乎需要整个 re -写作ggtern,所以拥有完美的主题是我最不优先考虑的事情。在最近的版本中,由于我有时间细化和识别问题,因此应该已经解决了这种行为——通过运行以下两 (2) 个命令(按此顺序,从干净的会话)证明了这一点,除了标题中的明显差异应该产生相同的结果:

library(ggplot2)
ggplot(data=mpg,aes(y=year,x=cyl)) + 
    geom_point() + theme_bw() + 
    labs(title='From ggplot2')

library(ggtern)
ggplot(data=mpg,aes(y=year,x=cyl)) + 
    geom_point() + theme_bw() + 
    labs(title='From ggtern')

ggplot2版本

ggternversion

如果我只能说,这似乎有点离题,但整个问题实际上最初源于一件事,通过几个程度的分离。ggplot2设计时没有剪裁蒙版,它使用网格视口作为伪剪裁蒙版,因为它的所有绘图(极坐标变换除外)都呈现在矩形区域上。位于矩形区域之外的数据映射随后被视口丢弃。ggtern另一方面需要一个,因为它需要在矩形视口内的三角形多边形区域内进行渲染——众所周知,“三角形不适合方孔”或者俗话说!当轴限制减少时,数据映射可以并且经常位于三角形区域之外,并且这些需要被删除或屏蔽(或者如果用户愿意则显示)。删除不是一种选择,因为它会影响某些图,如多边形、路径、密度或等高线图(我认为点几何图形可能是唯一不受潜在影响的图),这仅留下一个选项 - 剪贴蒙版的实现. 在一些早期版本中,没有实现剪贴蒙版(有利于对数据进行子集化),因此需要修改基本主题元素。如果有的话,也许是最初的“糟糕的设计”,在后来的许多代人中都流向了令人沮丧的行为。

于 2016-07-18T20:01:46.250 回答
1

我通过分离ggtern解决了我的问题。

detach("package:ggtern",unload=T)

于 2016-07-18T18:30:42.570 回答