dankelley / oce

R package for oceanographic processing
http://dankelley.github.io/oce/
GNU General Public License v3.0
145 stars 42 forks source link

imagep(): Plotting palettes issue #1527

Closed EOGrady21 closed 5 years ago

EOGrady21 commented 5 years ago

Short summary of problem

Palette is filling behind imagep() plot

Details (optional)

When exporting plots, the colorbar palette fills ~3/4 of the background of the plot

What you did

imagep(x = lon[1:100], y = depth[1:15], z = t(u[1:15,1:100]), flipy = T, filledContour = T, 
          xlab = 'Longitude', ylab = 'Depth (m)', main = 'U Velocity Component')

What you expected to happen

Plot 1 shows in display window in R studio

plot1

but Plot 2 is exported

plot2

How urgent is this?

Not urgent!

Output from sessionInfo()

R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

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] OceanView_1.0.4 plot3Drgl_1.0.1 rgl_0.100.19    plot3D_1.1.1    ncdf4_1.16      oce_0.9-24     
[7] gsw_1.0-5       testthat_2.0.0 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18            knitr_1.20              magrittr_1.5            misc3d_0.8-4           
 [5] xtable_1.8-3            lattice_0.20-35         R6_2.2.2                rlang_0.2.2            
 [9] tools_3.5.0             webshot_0.5.1           rgdal_1.3-4             grid_3.5.0             
[13] miniUI_0.1.1.1          htmltools_0.3.6         crosstalk_1.0.0         yaml_2.1.19            
[17] digest_0.6.16           shiny_1.2.0             later_0.8.0             htmlwidgets_1.3        
[21] promises_1.0.1          manipulateWidget_0.10.0 shape_1.4.4             mime_0.5               
[25] sp_1.3-1                compiler_3.5.0          jsonlite_1.5            httpuv_1.5.0 
dankelley commented 5 years ago

I think the problem has to do with resizing and margins (or, at least, some aspect of plot-device geometry). Other packages seem to have similar problems. I have never figured out a way to fix this. My solution is to always redraw the plot, if I resize the plot window.

Of course, what you say is that you are trying to 'export' the plot, but I think this involves resizing within Rstudio.

My suggested solution is simply to put a png() call before the plot (or a pdf() or whatever you like) and then a dev.off() call after the plot.

As for making 'export' work, I don't really know on that, sorry. Someone else in your working group may be more familiar with what Rstudio is doing.

Sorry, not much help ... except that having explicit png() etc calls is very useful in reproducibility. Otherwise, you may find it difficult to get the same plot on another day! I always write e.g.

if (!interactive()) png(...)
... plot commands
if (!interactive()) dev.off()

because then the .R code works fine interactively, but when you run it non-interactively, it makes the plots. Another approach is to put

externalPlot <- TRUE || !interactive()

at the start of the code, and then to test on that. This way, you can even create the files in an interactive session, just by changing that line.

EOGrady21 commented 5 years ago

Thanks for the tip, Dan! This solved my issue.

Cheers!

richardsc commented 5 years ago

I'm late, and you've figured it out, but I wanted to echo Dan's caution that the RStudio plot window can behave weirdly compared to the "regular" R plot windows. I often avoid using it for imagep() plots by using dev.new() when I'm working interactively.

Also, when we sit down to chat (though I see you are making progress without me ... :P) I can show you some tricks that you can use so that your plots are only saved to files when they are run a certain way, which means that if you are actively working on the code in an R session, you'll see the plots. But when it's done, you run it from outside of the active R session, and then the plots will be saved.

richardsc commented 5 years ago

Oh, I should have read the whole thread ... Dan already gave you some of those pointers.

🤷‍♂️