2013-06-30 9 views
5

की खाली पीएनजी छवि मैं देर से आर, आरएसवीआर और जीजीप्लॉट के साथ काम कर रहा हूं।रुपये से ggplot कॉलिंग। 1 केबी

आर कंसोल में निष्पादित होने पर कोड का यह टुकड़ा सीधे चार्ट को दो बार के साथ प्रस्तुत करता है। पीएनजी (फ़ाइल = 'Yash_GenderVsTotalAccountBalance.png', चौड़ाई = 400, ऊंचाई = 350, res = 72) ggplot (डेटा = YashCustomersAccounts, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'पहचान') dev.off()

लेकिन जब मैं जावा से एक ही कोड (जीजीप्लॉट कॉल शामिल करता हूं) को सुरक्षित करता हूं तो यह एक खाली पीएनजी बनाता है। कोड नीचे जैसा है।

package RRnD; 
import java.awt.*; 
import org.rosuda.REngine.*; 
import org.rosuda.REngine.Rserve.*; 

public class PlottingGenderVsTotalAccountBalance { 

    public static void main(String[] args) throws RserveException { 
     try { 
      RConnection c = new RConnection(); // make a new local connection on default port (6311) 
      System.out.println("1. Connection created ----------------------------------------------------------------------");    
      System.out.println("Working directory = "+c.eval("getwd()").asString()); 
      System.out.println("2. Working dir read ----------------------------------------------------------------------"); 
      c.eval("YashCustomers <- read.csv('YashCustomer.csv', header=TRUE)"); 
      c.eval("YashAccounts <- read.csv('YashAccount.csv', header=TRUE)"); 
      c.eval("YashCustomersAccounts <- merge(YashCustomers,YashAccounts, by='CUSTOMER_ID')"); 
      System.out.println("3. Data.frames read ----------------------------------------------------------------------"); 

      c.eval("library(ggplot2)"); 
      c.eval("require(ggplot2)"); 
      System.out.println("4. ggplot2 loaded ----------------------------------------------------------------------"); 

      c.eval("png(file='Yash_GenderVsTotalAccountBalance.png',width=400,height=350,res=72)"); 
      c.parseAndEval("ggplot(data=YashCustomersAccounts, aes(x=GENDER_DESC,y=ACCOUNT_BALANCE)) + geom_bar(stat='identity');dev.off()");    
      System.out.println("5. plotting done ----------------------------------------------------------------------"); 

      REXP xp = c.parseAndEval("r=readBin('Yash_GenderVsTotalAccountBalance.png','raw',1024*1024)"); 
      c.parseAndEval("unlink('Yash_GenderVsTotalAccountBalance.jpg'); r"); 
      Image img = Toolkit.getDefaultToolkit().createImage(xp.asBytes()); 
      System.out.println("img = "+img); 
      System.out.println("6. File reading done ----------------------------------------------------------------------"); 

      System.out.println("10. All done ----------------------------------------------------------------------");    
      c.close(); 
     } catch (REngineException ree) { 
      System.out.println("REngineException ..."); 
      System.out.println(ree.getMessage()); 
     } catch (Exception e) { 
      System.out.println("Exception ..."); 
      System.out.println(e.getMessage()); 
     } 
    } 

} 

नोट: - जीजीप्लॉट कॉल के बजाय, यदि मैं निम्नलिखित पंक्ति की तरह एक साधारण प्लॉट कॉल करता हूं, तो यह ठीक काम करता है। पीएनजी छवि ठीक से बनाया गया है। c.parseAndEval ("साजिश (यश ग्राहक ['CUSTOMER_ID']); dev.off()"); ... के बजाय ... c.parseAndEval ("ggplot (डेटा = YashCustomersAccounts, एईएस (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'पहचान'); dev.off()");

कृपया इस मुद्दे को खोजने में मेरी मदद करें। बहुत धन्यवाद, --Yash

+0

IMO आप '' जिसके परिणामस्वरूप ggplot2' वस्तु print' चाहिए। – daroczig

उत्तर

2

कंसोल ggplot में छवि स्वचालित रूप से खींचती है। लेकिन रुपये से ggplot साजिश को कॉल करते समय हमें छवि को स्पष्ट रूप से प्रिंट करने की आवश्यकता है।

c.parseAndEval ("प्रिंट (ggplot (डेटा = YashCustomersAccounts, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'पहचान')); dev.off()");

धन्यवाद, --Yash