2016年1月15日金曜日

rJava : プログラミング(7) Boxplotの生成

今度はデータベースのデータを使ってBoxplotを描いてみました。
MySQLクラスは同じです。

$ vi JRITest09.java


import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
import java.sql.*;
import java.util.Arrays;

public class JRITest09 {

        public static void main(String[] args)
        {
                MySQL mysql = new MySQL();
                Rengine re = new Rengine( new String[]{"--no-save"}, false, null );
                ResultSet rs = mysql.selectOperators();

                double awareness [] = new double[15];
                double decision [] = new double[15];
                double communication [] = new double[15];
                double leadership [] = new double[15];
                int index = 0;
                try {
                        while(rs.next()){
                                awareness[index] = rs.getDouble("awareness");
                                decision[index] = rs.getDouble("decision");
                                communication[index] = rs.getDouble("communication");
                                leadership[index] = rs.getDouble("leadership");
                                index ++;
                        }
                } catch (SQLException e) {
                        e.printStackTrace();
                }
                //System.out.println(Arrays.toString(result));

                StringBuffer buf = new StringBuffer();
                buf.append("awareness<-c(");
                for(int i = 0; i < awareness.length; i++){
                        if(i == awareness.length -1)
                                buf.append(awareness[i]+")");
                        else
                                buf.append(awareness[i]+",");
                }
                String c_value = buf.toString();
                re.eval(c_value);
                
                re.eval("decision<-c("+decision[0]+","+decision[1]+","+decision[2]+","+decision[3]+","+decision[4]+","+decision[5]+","+decision[6]+","+decision[7]+","+decision[8]+","+decision[9]+","+decision[10]+","+decision[11]+","+decision[12]+","+decision[13]+","+decision[14]+")");
                re.eval("communication<-c("+communication[0]+","+communication[1]+","+communication[2]+","+communication[3]+","+communication[4]+","+communication[5]+","+communication[6]+","+communication[7]+","+communication[8]+","+communication[9]+","+communication[10]+","+communication[11]+","+communication[12]+","+communication[13]+","+communication[14]+")");
                re.eval("leadership<-c("+leadership[0]+","+leadership[1]+","+leadership[2]+","+leadership[3]+","+leadership[4]+","+leadership[5]+","+leadership[6]+","+leadership[7]+","+leadership[8]+","+leadership[9]+","+leadership[10]+","+leadership[11]+","+leadership[12]+","+leadership[13]+","+leadership[14]+")");
                
                re.eval("png('boxplot.png', 640, 480)");
                re.eval("boxplot(awareness,decision,communication,leadership, main='BoxPlot for mean of each category',names = c('Awareness', 'Decision', 'Communication','Leadership'))");
                re.eval("dev.off()");
                re.end();
        }
}




配列生成のところはfor文でもやってもいいですが、そのまま書いた方がスペースが入らなくすみました。生成したグラフは、図のようになりました。
あとは
$ javac JRITest09.java
$ java -Djava.library.path=/usr/lib64/R/library/rJava/jri/ JRITest09

参考
http://www.ic.daito.ac.jp/~mizutani/R/spread/boxplot.html

0 件のコメント:

コメントを投稿