davidgohel / rvg

https://davidgohel.github.io/rvg/
132 stars 15 forks source link

Issues with rvg and ggplot2 #5

Closed itsdalmo closed 8 years ago

itsdalmo commented 8 years ago

Issues

  1. rvg creates a white panel background for ggplot2 objects (there is a rectangle inside the plots with a solid white fill), even though theme(panel.background = element_blank()) is specified for the plot. This did not use to be an issue before the latest release on CRAN. You have to look at the examples to see this issue.
  2. Peculiar output in ggplot2 when using geom_bar (possible other geoms as well) with manually specified limits on y-axis (possibly x-axis also). See pictures below, where PNG is the correct output.

skjermbilde2 skjermbilde

Example output

ReporteRs - blank_canvas.pptx RVG - blank_canvas.pptx ReporteRs - ylimited_regular barchart.pptx RVG - ylimited_regular barchart.pptx ReporteRs - ylimited_stacked barchart.pptx RVG - ylimited_stacked barchart.pptx

Example code

library(ggplot2)

plot_to_pptx <- function(plt, title) {
  doc <- ReporteRs::pptx()
  fname <- paste0(sub("\\s", "_", tolower(title)), ".pptx")

  # ReporteRs (vector)
  doc <- ReporteRs::addSlide(doc, slide.layout = "Title and Content")
  doc <- ReporteRs::addTitle(doc, title)
  doc <- ReporteRs::addPlot(doc, fun = print, x = plt, vector.graphic = TRUE)

  # ReporteRs (png)
  doc <- ReporteRs::addSlide(doc, slide.layout = "Title and Content")
  doc <- ReporteRs::addTitle(doc, title)
  doc <- ReporteRs::addPlot(doc, fun = print, x = plt, vector.graphic = FALSE)

  # Save 
  ReporteRs::writeDoc(doc, file = paste0("ReporteRs - ", fname))

  # rvg
  rvg::write_pptx(paste0("RVG - ", fname), print(plt))
  invisible()
}

#1 - Blank canvas not working
plt <- ggplot(data = mtcars, aes(x = hp, y = mpg))
plt <- plt + geom_point() + theme_bw() + theme(panel.background = element_blank(), plot.background = element_blank())
plot_to_pptx(plt = plt, title = "Blank Canvas")

#2 - stacked barchart
library(dplyr)
df <- mtcars %>%
  mutate(cyl = factor(paste(cyl, "cyl"), levels = paste(c(4, 6, 8), "cyl"))) %>%
  mutate(am = factor(ifelse(am == 0L, "No", "Yes"))) %>%
  group_by(am, cyl) %>%
  summarise(mpg = mean(mpg))

plt <- ggplot(data = df, aes(x = cyl, y = mpg, group = am, fill = am)) + geom_bar(stat = "identity") 
plot_to_pptx(plt = plt, title = "Stacked barchart")

# With ylimit
plt <- plt + scale_y_continuous(limits=c(10L, 50L), oob = scales::rescale_none) 
plot_to_pptx(plt = plt, title = "Ylimited stacked barchart")

#3 - Regular barchart
plt <- ggplot(data = df, aes(x = cyl, y = mpg, group = am, fill = am)) + 
  geom_bar(stat = "identity", position = position_dodge(width = 0.6))
plot_to_pptx(plt = plt, title = "Regular barchart")

# With ylimit
plt <- plt + scale_y_continuous(limits=c(10L, 50L), oob = scales::rescale_none) 
plot_to_pptx(plt = plt, title = "Ylimited regular barchart")
davidgohel commented 8 years ago

Hi

Sorry, I can see the issue in the pptx you sent but I can't reproduce your issue:

About the panel background, I can't see any differences between png and vector graphics, but if you want a transparent background, use bg = "transparent" with addPlot.

itsdalmo commented 8 years ago

Hi again,

Very sorry, I forgot to add scales:: in front of rescale_none. Updated the example code in the original post. I also managed to reproduce the problem on a new Windows computer (sessioninfo below).

Thanks for clearing up the problem I had with transparent backgrounds also; setting bg = "transparent" fixed it. This is a new argument to addPlot, correct?

Session info (2nd computer)

R version 3.2.1 (2015-06-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=Norwegian Bokmål_Norway.1252  LC_CTYPE=Norwegian Bokmål_Norway.1252   
[3] LC_MONETARY=Norwegian Bokmål_Norway.1252 LC_NUMERIC=C                            
[5] LC_TIME=Norwegian Bokmål_Norway.1252    

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

other attached packages:
[1] ggplot2_2.1.0    dplyr_0.4.3.9000 gdtools_0.0.7   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3          magrittr_1.5         knitr_1.12.3         xml2_0.1.2           ReporteRsjars_0.0.2 
 [6] munsell_0.4.3        colorspace_1.2-6     xtable_1.8-2         R6_2.1.2             plyr_1.8.3          
[11] tools_3.2.1          parallel_3.2.1       grid_3.2.1           gtable_0.2.0         rvg_0.0.7           
[16] R.oo_1.20.0          png_0.1-7            DBI_0.3.1            htmltools_0.3        lazyeval_0.1.10.9000
[21] assertthat_0.1       digest_0.6.9         rJava_0.9-8          shiny_0.13.1         ReporteRs_0.8.5     
[26] R.utils_2.2.0        mime_0.4             labeling_0.3         openxlsx_3.0.0       scales_0.4.0        
[31] R.methodsS3_1.7.1    httpuv_1.3.3   
davidgohel commented 8 years ago

ok thanks, I can reproduce your issue

davidgohel commented 8 years ago

yes, bg is a new argument, it is used by rvg device

davidgohel commented 8 years ago

Sorry for that, (bug with rectangle clipping)... It should be ok now. Could you try with the github version please?

David

itsdalmo commented 8 years ago

Tested it now, and it checks out for all the examples I made - as well as some other slides. Thank you very much!.

Ps while I have your attention: ReporteRs states that addMarkdown is depreciated. What is the replacement?

davidgohel commented 8 years ago

cool

Yes, addMarkdown is deprecated, I will try to replace it with commonmark package later