create ggplot2 graphics in Java using R

1,408 views
Skip to first unread message

Rubén Turanzas

unread,
Jun 23, 2014, 2:44:24 PM6/23/14
to ggp...@googlegroups.com
Hello,

I have create a desktop application in Java using Swing. I would like to generate a heatmap graphic to show it in my app as an image so I installed rJAva and through JRI it connects to R.
I wrote my R script in R for generating and saving to hard disk my ggplot2 heatmap graphic. So far so good, Java connects with R perfectly in the java side. In hte other side, R creates the image perfectly in Rstudio. 
The problem exists when I connect from Java (through rJAva/JRI libraries) to R, to generate the ggplot2 graphics. It only generates a blank png. If it were a plot graphic instead of a ggplot2 graphic it wouldn´t have any problem.

My Java code which connects to R:
File mapa = new File("D:\\trading_tools\\javaBBB\\BBBproject\\files\\mapas\\cosa.png");
if(!mapa.exists()){
    Rengine re=new Rengine (new String []{"--vanilla"}, false, null);
    if (!re.waitForR())  {
        System.out.println ("Cannot load R");
        return;
    }
    writeCSV(); //function which wrotes 'test2csv.csv' in hard disk
    re.eval("require(ggplot2);require(XML);require(rjson)");
    re.eval("csv<-read.csv('D:/trading_tools/javaBBB/BBBproject/files/mapas/test2csv.csv',head=T,sep=';',dec='.',colClasses=rep('numeric', 24))");
    re.eval("png(filename='D:/trading_tools/javaBBB/BBBproject/files/mapas/cosa.png', width = 400, height = 400)");
    re.eval("g<-ggplot(csv[1:nrow(csv),], aes(x=sl,y=tp)) + geom_tile(aes(fill=CA))");
    re.eval("dev.off()");    
    re.end();
}

I read in some webs that ggplot2 doesn´t generate the graphics out of R. 
Can sombebody help me please? Any workaround or different option?  
I thought about converting the project in a web application and then use Rserve but I am afraid of doing all the work to finally have the same issue in Rserve.
Does anybody have experience in this issue?

Any suggestion or help will be appreciatted! 
Thanks in advance and best regards.

Upton, Stephen (Steve) (CIV)

unread,
Jun 24, 2014, 7:52:36 AM6/24/14
to Rubén Turanzas, ggp...@googlegroups.com

Hi,

 

I think you actually have to print “g” to plot it, or just use ggsave.

 

steve

 

Stephen C. Upton
Research Associate
SEED (Simulation Experiments & Efficient Designs) Center
Naval Postgraduate School

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kranthikumar Kandi

unread,
Jul 25, 2016, 6:29:44 PM7/25/16
to ggplot2
Hi Turanzas

I am also working on rJava and ggplot. i am using print to see the outplot plots in RStudio.

how can i create a plot and show on html page other than jpn or png, bcz the plots are interactive i.e. on mouse hover showing some data.

Andrés Hidalgo

unread,
Oct 14, 2016, 6:39:32 PM10/14/16
to ggplot2
Hola Ruben. Tengo exactamente el mismo problema que planteabas en este "post" de hace dos años. Lograste resolverlo? Si así fue, ¿cómo los hiciste? Esto me ayudaría muchísimo. Saludos
Reply all
Reply to author
Forward
0 new messages