Scatter
// This example was found at:
// www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)
val rand = java.util.Random(123)
val n = 20
val dataset = dataFrameOf(
"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 cond = "cond"<String>()
val xvar = "xvar"<Int>()
val yvar = "yvar"<Double>()
Basic Scatter Plot
plot(dataset) {
points {
x(xvar)
y(yvar)
symbol = Symbol.CIRCLE_OPEN
}
}
plot(dataset) {
points {
x(xvar)
y(yvar)
color(cond)
symbol(cond)
size = 5.0
}
layout {
size = 700 to 350
}
}
plot(dataset) {
points {
x(xvar)
y(yvar)
color(cond)
symbol(cond) {
scale = categorical(range = listOf(Symbol.CIRCLE_OPEN, Symbol.TRIANGLE_OPEN))
}
size = 5.0
}
layout {
size = 700 to 350
}
}
Handling Over-plotting
// Create data with overlapping points.
val datasetOverlapping = dataset.convert { xvar and yvar }.with {
(it.toDouble() / 5).toInt() * 5
}
plot(datasetOverlapping) {
points {
x(xvar) {
axis.breaks(listOf(0, 5, 10, 15))
}
y(yvar)
alpha = .3
size = 7.0
}
layout {
size = 700 to 350
}
}
plot(datasetOverlapping) {
points {
x(xvar) {
axis.breaks(listOf(0, 5, 10, 15))
}
y(yvar)
symbol = Symbol.CIRCLE_OPEN
position = Position.jitter(.1, .1)
}
layout {
size = 700 to 350
}
}
Last modified: 07 December 2023