java - 从 Rserve 调用 ggplot。 1KB 的空白 png 图像

标签 java r ggplot2 png rserve

<分区>

我最近一直在使用 r、rserve 和 ggplot。

这段代码在 R 控制台中执行时会按预期直接呈现包含两个条形图的图表。 png(文件='Yash_GenderVsTotalAccountBalance.png',宽度=400,高度=350,res=72) ggplot(data=YashCustomersAccounts, aes(x=GENDER_DESC,y=ACCOUNT_BALANCE)) + geom_bar(stat='身份') dev.off()

但是当我使用 Rserve 从 JAVA 调用相同的代码(涉及 ggplot 调用)时,它会创建一个空白的 png。 代码如下。

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());
        }
    }

}

注意:- 如果我不使用 ggplot 调用,而是像下面一行那样进行简单的绘图调用,它就可以正常工作。 png 图像已正确创建。 c.parseAndEval("plot(YashCustomers['CUSTOMER_ID']);dev.off()"); ... 而不是 ... c.parseAndEval("ggplot(data=YashCustomersAccounts, aes(x=GENDER_DESC,y=ACCOUNT_BALANCE)) + geom_bar(stat='identity');dev.off()");

请帮助我找出问题所在。 非常感谢, --亚什

最佳答案

在控制台中,ggplot 会自动绘制图像。 但是在从 Rserver 调用 ggplot 绘图时,我们需要显式打印图像。

c.parseAndEval("print(ggplot(data=YashCustomersAccounts, aes(x=GENDER_DESC,y=ACCOUNT_BALANCE)) + geom_bar(stat='identity'));dev.off()");

关于java - 从 Rserve 调用 ggplot。 1KB 的空白 png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17395651/

相关文章:

java - 比较变量时前缀和后缀递增

r - 如何在ggplot2中将标签居中放置在并排条形图上

java - 以 json 或 xml 格式在文件中写入类的实例

java - Java 中的手动列表改组每次都会产生相同的结果

r - 将DD-MMM-YY形式的字符转换为R中的DD/MM/YY

algorithm - R 结构中的节数

r - ggplot : “Error in grid.Call(” L_textBounds“, as.graphicsAnnot(x$label), x$x, x$y, … 中出现错误问题”

r - abline 和 stat_smooth 的 ggplot2 图例

r - R中有没有办法用两个轴绘制图例?

java - 为什么 != 在 if 语句的前面和在结尾处不一样?