%useLatestDescriptors
%use lets-plot
// %use krangl
LetsPlot.getInfo() // This prevents Krangl from loading an obsolete version of Lets-Plot classes.
Lets-Plot Kotlin API v.4.1.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.2.5.1.
%use krangl
var mpg = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
mpg.head()
manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
4 | audi | a4 | 2.0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
5 | audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
Shape: 5 x 12.
letsPlot(mpg.toMap()) + geomViolin {y = "hwy"}
geomViolin()
and geomDesity()
¶val densityPlot = letsPlot(mpg.toMap()) +
geomDensity(color="black", alpha=.5) {x="hwy"; fill="drv"} +
facetGrid(x="drv") +
coordFlip() + scaleYContinuous(breaks = emptyList())
ggtitle("geomDensity()")
val violinPlot = letsPlot(mpg.toMap()) +
geomViolin(alpha=0.5) {
x = asDiscrete("drv", order=1)
y = "hwy"
fill = "drv"
} +
ggtitle("geomViolin()")
gggrid(listOf(densityPlot, violinPlot), 2, 400, 300)
drawQuantiles
¶val DRAW_QUANTILES = listOf(.25, .5, .75)
letsPlot(mpg.toMap()) { x = "drv"; y = "hwy" } +
geomViolin(drawQuantiles=DRAW_QUANTILES) +
ggtitle("drawQuantiles=$DRAW_QUANTILES")
scale
¶val options = listOf(null, "area", "count", "width")
val plots = List(options.size) { i ->
letsPlot(mpg.toMap()) { x = "drv"; y = "hwy" } +
geomViolin(scale = options[i], drawQuantiles=DRAW_QUANTILES) +
ggtitle("scale = ${options[i]} ")
}
gggrid(plots, 2, 400, 250)
ydensity
statistic¶val plotBase = letsPlot(mpg.toMap()) { x = "drv"; y = "hwy" }
val plots1 = listOf(
plotBase + geomViolin(drawQuantiles=DRAW_QUANTILES) + ggtitle("Default"),
plotBase + geomViolin(drawQuantiles=DRAW_QUANTILES, kernel="epanechikov") + ggtitle("kernel='epanechikov'"),
plotBase + geomViolin(drawQuantiles=DRAW_QUANTILES, bw=.1) + ggtitle("bw=0.1"),
plotBase + geomViolin(drawQuantiles=DRAW_QUANTILES, adjust=2) + ggtitle("adjust=2"),
)
gggrid(plots1, 2, 400, 250)
val plotBase2 = plotBase +
scaleColorBrewer(type="qual", palette="Set1") +
scaleFillBrewer(type="qual", palette="Set1") +
themeGrey()
plotBase2 +
geomViolin(drawQuantiles = DRAW_QUANTILES, alpha = 0.5, size = 2,
tooltips = layerTooltips().title("^x")
.line("year|@year")
.line("hwy|@hwy")
.line("violinwidth|@..violinwidth..")
.line("density|@..density..").format("@..density..", ".3f")
.line("count|@..count..").format("@..count..", ".3f")
.line("scaled|@..scaled..").format("@..scaled..", ".3f")
) {
color = asDiscrete("year")
fill = asDiscrete("year")
}
plotBase2 +
geomViolin(drawQuantiles = DRAW_QUANTILES, alpha = 0.5, size = 2) {
color = asDiscrete("year")
fill = asDiscrete("year")
} +
facetGrid(y = "year")
coord_flip()
¶plotBase + geomViolin(drawQuantiles = DRAW_QUANTILES) + coordFlip()
geomViolin()
and geomBoxplot()
¶plotBase2 +
geomViolin(drawQuantiles = DRAW_QUANTILES, alpha = 0.5, size = 1, trim = false) +
geomBoxplot(width=.2, position = positionDodge(.2), outlierShape = 21, outlierFill = "white", outlierSize = 5) {
color = asDiscrete("year")
fill = asDiscrete("year")
}