%use lets-plot
%use numpy
import org.jetbrains.numkt.core.*
import org.jetbrains.numkt.math.*
import org.jetbrains.numkt.random.Random
import org.jetbrains.numkt.*
val cov0 = array<Double>(listOf(listOf(1, -.8),
listOf(-.8, 1)))
val cov1 = array<Double>(listOf(listOf(1, .8),
listOf(.8, 1)))
val cov2 = array<Double>(listOf(listOf(10, .1),
listOf(.1, .1)))
val n = intArrayOf(400)
val xy0 = Random.multivariateNormal(mean=array(listOf(-2,0)),
cov=cov0,
size=n,
checkValid="raise",
tol=1e-8
).transpose()
val xy1 = Random.multivariateNormal(mean=array(listOf(2,0)),
cov=cov1,
size=n,
checkValid="raise",
tol=1e-8
).transpose()
val xy2 = Random.multivariateNormal(mean=array(listOf(0,1)),
cov=cov2,
size=n,
checkValid="raise",
tol=1e-8
).transpose()
val data = mapOf(
"x" to concatenate(xy0[0], xy1[0], xy2[0]).toList(),
"y" to concatenate(xy0[1], xy1[1], xy2[1]).toList()
)
val p = ggplot(data) {x="x"; y="y"} + ggsize(600,300) +
geom_point(color="black", alpha=.1)
p
// Basic density
p + geom_density2d(color="red")
// Set contour color by level
// - change defailt position and size of colorbar
p + geom_density2d {color="..level.."} +
scale_color_gradient(low="dark_green", high="yellow", guide=guide_colorbar(barHeight=10, barWidth=300)) +
theme().legendPosition_bottom()
val p1 = ggplot(data) {x="x"; y="y"} + ggsize(600,300)
// Filled polygons are not always working well - note missing polygons in the middle.
p1 + geom_polygon(stat=Stat.density2d()) {fill="..level.."} + coord_fixed()
// 'density2df' is not dependent on poligons order and works a lot better
p1 + geom_density2df {fill="..level.."}
p1 + geom_bin2d()
// Adjust the tile size - make them square and bigger.
// Show density instead of count.
p1 + geom_bin2d(binWidth=1 to 1) {fill="..density.."}