ggtern disables some themes from ggplot2

。_饼干妹妹 提交于 2021-01-27 22:18:17

问题


Trying to switch the theme to theme_bw() is not working, theme_grey() seems to be prioritized. Any ideas why?

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

EDIT:

As suggested in the comments, this works when run in a clean R session. But when I implement the code in a session with ggtern loaded, the problem crops up.

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    

Looks like ggtern masks a lot of the themes from ggplot2.


回答1:


ggtern masks EVERY default theme from ggplot2, and this is because when writing this software, some ~60 new theme elements have been created, which exist in order to make ggtern render correctly. For a comprehensive list of the new theme elements, run the following command:

library(ggtern)
?theme_elements

Having said the above, I was aware of some clashes, as you have identified, due to ggtern also modifying some of the base theme elements, and since about version 2.1.2 - 2.1.3, I have completely re-worked the themes so that ggtern no longer modifies any of the base elements -- hopefully this annoying behaviour has now gone away for good!

I am in the process of producing a publication on the package, and have been refining many many many long-tern annoyances, so please download and install the most recent version (2.1.4) from my (Bitbucket Repository). Embarrassingly, even the last 2.1.3 version on CRAN, I picked up on a pretty significant bug when running two plots in a grid.arrange type situation. This is an issue with the clipping mask, and something which has been resolved in 2.1.4 yet sent to CRAN, something I plan to do imminently.

Anyway, to answer your question, please upgrade your version, you are running version 2.1.0, which was a version released not long after a HUGE revision in ggplot2 -- in fact, not long earlier, prior versions were so heavily broken, requiring almost the entire re-writing of ggtern, so having perfect themes was the least of my priorities. In recent versions, since I have had time to refine and identify issues, this behaviour should have been resolved -- demonstrated by the following two (2) commands run (in this order, from a clean session), which, with the exception of the obvious difference in the titles, should yield identical results:

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')

If I can just say, this may seem a little off-topic, but this whole problem actually originally stemmed, by a couple of degrees of separation, from one thing. ggplot2 was designed without clipping masks, it uses the grid viewport as essentially a pseudo clipping mask, because all of its plots (with the exception of the polar transformation), are rendered on a rectangular region. Data mappings that lie outside of the rectangular region are subsequently discarded by the viewport. ggtern on the other hand needs one, because it needs to render within a triangular shaped polygon region housed within a rectangular viewport -- and as everyone knows, 'triangles don't fit in square holes' or however the saying goes! When the axis limits are reduced, data mappings can, and often do, lie outside of the triangular region, and these need to be either deleted or masked (or shown if the user so wishes). Deleting is not an option, since it would then affect certain plots like polygon, path, density or contour plots (I think probably point geometries are the only ones which are not potentially affected), which leaves only one option -- implementation of clipping mask. In some of the earlier versions, a clipping mask wasn't implemented (in favour of subsetting the data) thus requiring modifications of base theme elements. If anything, perhaps an initial 'poor design', has flowed on many generations later to a frustrating behaviour.




回答2:


ggtern masks theme_bw(), meaning that ggtern has its own version of theme_bw() that overrides the ggplot2 version. Oddly, ggtern's version of theme_bw() looks like theme_grey(), so maybe that's a bug.

If you want to keep ggtern loaded, another option is to enforce the ggplot2 version of theme_bw():

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

Unfortunately, you can't just load ggplot2 after loading ggtern (to avoid the masking issue) because ggtern loads ggplot2.

To see the difference in the code for each version of theme_bw() type the following in the console:

ggplot2::theme_bw

ggtern::theme_bw

UPDATE: The above clash between themes in ggplot2 and ggtern occurred in ggtern version 2.1.2 and earlier, but the clash no longer occurs in version 2.1.3, so all you need to do is update to the latest version of ggtern. See @NicolasHamilton's answer for full details.




回答3:


I solved my problem by detaching ggtern.

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



来源:https://stackoverflow.com/questions/38442603/ggtern-disables-some-themes-from-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!