%useLatestDescriptors
%use lets-plot
import java.util.Random
// This example was found at:
// www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)
val rand = java.util.Random(123)
val n = 20
val data = mapOf<String, List<*>>(
"cond" to List(n / 2) { "A" } + List(n / 2) { "B" },
"xvar" to List(n) { i:Int-> i },
"yvar" to List(n) { i:Int-> i + rand.nextGaussian() * 3 }
)
val p = letsPlot(data) { x = "xvar"; y = "yvar" } + ggsize(300, 250)
p + geomPoint(shape = 1)
p + geomPoint(shape = 1) +
geomSmooth(seed = 42)
// Without standard error band.
p + geomPoint(shape = 1) +
geomSmooth(se = false, seed = 42)
cond
variable¶val p1 = letsPlot(data) { x = "xvar"; y = "yvar"; color = "cond" } + ggsize(500, 250)
p1 + geomPoint(shape = 1) +
geomSmooth(se = false, seed = 42)
// Map `shape` to the `cond` variable.
p1 + geomPoint(size = 5) { shape = "cond" }
// Choose different shapes using `scale_shape_manual`:
// 1 - hollow circle
// 2 - hollow triangle
p1 + geomPoint(size = 5) { shape = "cond" } +
scaleShapeManual(values = listOf(1,2))
// Create data with overlapping points.
val data1 = mapOf(
"xvar" to (data["xvar"] as List<Double>).map { (it / 5).toInt() * 5 },
"yvar" to (data["yvar"] as List<Double>).map { (it / 5).toInt() * 5 },
)
val p2 = letsPlot(data1) { x = "xvar"; y = "yvar"} + ggsize(500, 250) +
scaleXContinuous(breaks = listOf(0, 5, 10, 15))
// Use `alpha` to show overplotting.
p2 + geomPoint(alpha = .3, size = 7)
// `jitter` points to show overplotting in another way.
p2 + geomPoint(shape = 1, position = positionJitter(width=.1, height=.1, seed = 42))