JuliaからRを使う

やはりggplotを使いたいという時がある.

]でパッケージモードに入り, addで追加.

(v1.4) pkg> add RCall
julia> using RCall, Distributions, Random, DataFrames

julia> Random.seed!(0)
MersenneTwister(UInt32[0x00000000], Random.DSFMT.DSFMT_state(Int32[748398797, 1073523691, -1738140313, 1073664641, -1492392947, 1073490074, -1625281839, 1073254801, 1875112882, 1073717145943540191, 1073626624, 1091647724, 1073372234, -1273625233, -823628301, 835224507, 991807863, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.00.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt128[0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x000000000000000000000000000000000x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000], 1002, 0)

julia> dat = rand(MvNormal([1 0.75; 0.75 1]), 1000)
2×1000 Array{Float64,2}:
 0.679107  -0.353007  0.586617   0.0649475  -0.51421   -0.688907  0.397482  -0.3463550.791341  -0.52823  -1.01701    0.300868  0.398911  0.831824  1.75993  -0.342151 
 1.05727   -0.353953  0.636632  -0.0233976   0.655664  -1.02123   0.834954  -0.383834     0.296767  -1.07364  -0.963753  -0.160464  0.109914  2.31958   1.47322   0.0729502

julia> df = DataFrame(permutedims(dat))

R""で内容を囲むと、R内で実行できる

julia> R"1+1"
RObject{RealSxp}
[1] 2

$を打つとRモードに入れる (なぜかこの機能はstartup.jlusing RCallを入れてREPLを起動しても働かない)

R> install.packages("viridis")

Rのライブラリを読み込む.

julia> @rlibrary ggplot2
julia> @rlibrary viridis

ggplotでプロットする.

julia> ggplot(df,aes(x=:x1,y=:x2)) + geom_point()

RDatasetsを使えば、irisデータ等も使える.

julia> using RDatasets
julia> iris = dataset("datasets", "iris")

julia> ggplot(iris,aes(x=:SepalWidth,y=:PetalWidth,size=:SepalLength,col=:Species)) + 
        geom_point()

f:id:dxsato:20200427124535j:plain

julia> ggplot(iris,aes(x=:SepalWidth,y=:PetalWidth,col=:SepalLength,shape=:Species)) + 
       geom_point(size=3,alpha=.5) +
       scale_color_viridis(option = "A",direction=-1) +
       theme_minimal()

f:id:dxsato:20200427124605j:plain