%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 = lets_plot(data) { x = "rating" } + ggsize(500, 250)
p + geom_histogram(binWidth=0.5)
// Histogram overlaid with kernel density curve
// - histogram with density instead of count on y-axis
// - overlay with transparent density plot
p + geom_histogram(binWidth=0.5, color="black", fill="white") { y = "..density.." } +
geom_density(alpha=0.2, fill=0xFF6666)
p + geom_histogram(binWidth=.5, color="black", fill="white") +
geom_vline(xintercept=(data["rating"] as List<Double>).average(), color="red", linetype="dashed", size=1.0)
val p1 = lets_plot(data) {x = "rating"; fill="cond"} + ggsize(500, 250)
// Default histogram (stacked)
p1 + geom_histogram(binWidth=0.5, alpha=.5)
// Overlaid histograms
p1 + geom_histogram(binWidth=0.5, alpha=0.5, position=Pos.identity)
// Interleaved histograms
p1 + geom_histogram(binWidth=0.5, alpha=.5, position=Pos.dodge)
// Density plot
val p2 = ggplot(data) {x="rating"; color="cond"} + ggsize(500, 250)
p2 + geom_density()
// Density plot with semi-transparent fill
p2 + geom_density(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 + geom_histogram(alpha=.3, position=Pos.identity, size=0.0, bins=10) {fill="cond"} +
geom_vline(data=cdat, linetype="dashed", size=1.0) {xintercept="rating"; color="cond"}
// Use frqpoly instead of histogram
p2 + geom_freqpoly(bins=10) {color="cond"} +
geom_vline(data=cdat, linetype="dashed", size=1.0) {xintercept="rating"; color="cond"}
// Density plots with means
p2 + geom_density() +
geom_vline(data=cdat, linetype="dashed", size=1.0) {xintercept="rating"; color="cond"}
ggplot(data) {x="rating"} +
geom_histogram(binWidth=.5, color="black", fill="white") +
facet_grid("cond")
// A basic box plot
val p3 = ggplot(data) {x="cond"; y="rating"} + ggsize(300, 200)
p3 + geom_boxplot()
// A basic box with the conditions colored
p3 + geom_boxplot {fill="cond"}
// Style outliers
p3 + geom_boxplot(outlierColor="red", outlierShape=8, outlierSize=5)