%useLatestDescriptors
%use lets-plot
import java.util.Random
val rand = java.util.Random(123)
val n = 200
val data = mapOf<String, Any>(
"cond" to List(n) { "A" } + List(n) { "B" },
"rating" to List(n) { rand.nextGaussian() } + List(n) { rand.nextGaussian() * 1.5 + 1.5 },
)
// Basic histogram of "rating"
val p = letsPlot(data) { x = "rating" } + ggsize(500, 250)
p + geomHistogram(binWidth=0.5)
// Histogram overlaid with kernel density curve
// - histogram with density instead of count on y-axis
// - overlay with transparent density plot
p + geomHistogram(binWidth=0.5, color="black", fill="white") { y = "..density.." } +
geomDensity(alpha=0.2, fill=0xFF6666)
p + geomHistogram(binWidth=.5, color="black", fill="white") +
geomVLine(xintercept=(data["rating"] as List<Double>).average(), color="red", linetype="dashed", size=1.0)
val p1 = letsPlot(data) {x = "rating"; fill="cond"} + ggsize(500, 250)
// Default histogram (stacked)
p1 + geomHistogram(binWidth=0.5)
// Overlaid histograms
p1 + geomHistogram(binWidth=0.5, alpha=0.7, position=positionIdentity)
// Interleaved histograms
p1 + geomHistogram(binWidth=0.5, position=positionDodge())
// Density plot
val p2 = ggplot(data) {x="rating"; color="cond"} + ggsize(500, 250)
p2 + geomDensity()
// Density plot with semi-transparent fill
p2 + geomDensity(alpha=.3) {fill="cond"}
// Find the mean of each group
val means = (data["cond"] as List<String> zip data["rating"] as List<Double>)
.groupBy(keySelector = { it.first }, valueTransform = { it.second })
.mapValues { it.value.average() }
val cdat = mapOf(
"cond" to means.keys,
"rating" to means.values
)
cdat
{cond=[A, B], rating=[-0.011843241476365302, 1.5547269440141214]}
// Overlaid histograms with means
p2 + geomHistogram(alpha=.3, position=positionIdentity, size=0.0, bins=10) {fill="cond"} +
geomVLine(data=cdat, linetype="dashed", size=1.0) {xintercept="rating"; color="cond"}
// Use frqpoly instead of histogram
p2 + geomFreqpoly(bins=10) {color="cond"} +
geomVLine(data=cdat, linetype="dashed", size=1.0) {xintercept="rating"; color="cond"}
// Density plots with means
p2 + geomDensity() +
geomVLine(data=cdat, linetype="dashed", size=1.0) {xintercept="rating"; color="cond"}
ggplot(data) {x="rating"} +
geomHistogram(binWidth=.5, color="black", fill="white") +
facetGrid("cond")
// A basic box plot
val p3 = ggplot(data) {x="cond"; y="rating"} + ggsize(300, 200)
p3 + geomBoxplot()
// A basic box with the conditions colored
p3 + geomBoxplot {fill="cond"}
// Style outliers
p3 + geomBoxplot(outlierColor="red", outlierShape=8, outlierSize=5)