%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_df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
mpg_df.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.
val mpg_dat = mpg_df.toMap()
val p = letsPlot(mpg_dat) {x="displ"; y="cty"} +
scaleSize(range = 5 to 15, breaks = listOf(15, 40)) + ggsize(600, 350)
// Default tooltips.
p + geomPoint(shape=21, color="white") {fill="drv"; size="hwy"}
// No tooltips.
p + geomPoint(shape=21, color="white", tooltips=tooltipsNone) {fill="drv"; size="hwy"}
// Change format for the "size" aesthetic which is already shown in the tooltip by default.
p + geomPoint(shape=21,
color="white",
tooltips=layerTooltips().format("^size", "{.0f} mpg")) {fill="drv"; size="hwy"}
// Show the vehicle "class" value in the tooltip (instead of the value of the "size" aesthetic).
p + geomPoint(shape=21,
color="white",
tooltips=layerTooltips().line("@class")) {fill="drv"; size="hwy"}
// Configure a multiline tooltip.
p + geomPoint(shape=21,
color="white",
tooltips=layerTooltips()
.format("cty", ".0f")
.format("hwy", ".0f")
.format("drv", "{}wd")
.line("@manufacturer @model")
.line("cty/hwy [mpg]|@cty/@hwy")
.line("@|@class")
.line("drive train|@drv")
.line("@|@year")) {fill="drv"; size="hwy"}
// List of variables to place in a multiline tooltip with the default formatting.
p + geomPoint(shape=21,
color="white",
tooltips=layerTooltips("manufacturer", "model", "class", "year")
) {fill="drv"; size="hwy"}
// Define the format for the variable from the list and specify an additional line.
p + geomPoint(shape=21,
color="white",
tooltips=layerTooltips("manufacturer", "model", "class", "drv")
.format("drv", "{}wd")
.line("cty/hwy [mpg]|@cty/@hwy")
) {fill="drv"; size="hwy"}
// Anchor the tooltip in the top-right corner of the plot.
p + geomPoint(shape=21,
color="white",
tooltips=layerTooltips()
.anchor("top_right")
.minWidth(180)
.format("cty", ".0f")
.format("hwy", ".0f")
.format("drv", "{}wd")
.line("@manufacturer @model")
.line("cty/hwy [mpg]|@cty/@hwy")
.line("@|@class")
.line("drive train|@drv")
.line("@|@year")) {fill="drv"; size="hwy"}
val p2 = letsPlot(mpg_dat) {x="class"; y="hwy"} + theme().legendPositionNone() + ggsize(600, 350)
// Default tooltips
p2 + geomBoxplot()
// Configure text in outlier tootips using the 'format()' function.
p2 + geomBoxplot(tooltips=layerTooltips()
.format("^Y", "{.0f}") // all Y-positionals (note: no 'labels')
.format("^middle", ".2f") // different precision for 'middle' (note: default 'label')
.format("^ymin", "min: {}") // ymin/ymax aesthetics:
.format("^ymax", "max: {}")) // - add custom 'label'
// Replace "side" tooltips with anchored (top_center) "general" tooltip.
// The 'line()' function assigns aesthetic or 'variable' to a general multiline tooltip.
p2 + geomBoxplot(tooltips=layerTooltips()
.anchor("top_center")
.format("^Y", ".0f")
.format("^middle", ".2f")
.line("min/max|^ymin/^ymax")
.line("lower/upper|^lower/^upper")
.line("@|^middle"))
// By default tooltip never shows values defined via layer parameters (constants).
// Still, these values can be added to a layer tooltip using the 'layer_tooltips()' function.
val rand = java.util.Random()
val n = 100
val dat = mapOf<String, Any>(
"x" to List(n) { rand.nextGaussian() },
"y" to List(n) { rand.nextGaussian() }
)
letsPlot(dat) {x="x"; y="y"} +
geomPoint() +
geomVLine(xintercept=(dat["x"] as List<Double>).average(), color="red", linetype="dashed", size=1,
tooltips=layerTooltips().format("^xintercept", ".4f").line("mean = ^xintercept"))
val iris_df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/iris.csv")
iris_df.head()
sepal_length | sepal_width | petal_length | petal_width | species |
---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | setosa |
4.9 | 3.0 | 1.4 | 0.2 | setosa |
4.7 | 3.2 | 1.3 | 0.2 | setosa |
4.6 | 3.1 | 1.5 | 0.2 | setosa |
5.0 | 3.6 | 1.4 | 0.2 | setosa |
Shape: 5 x 5.
val iris_dat = iris_df.toMap()
// Default density plot.
letsPlot(iris_dat) + ggsize(650, 300) +
geomArea(stat=Stat.density(), color="white") {x="sepal_length"; fill="species"}
// Change the tooltip content.
letsPlot(iris_dat) + ggsize(650, 300) +
geomArea(stat=Stat.density(),
color="white",
tooltips=layerTooltips()
.anchor("top_right")
.line("^fill")
.line("length|^x")
.line("density|^y")) {x="sepal_length"; fill="species"}
// Use '..density..' variable in the tooltip
letsPlot(iris_dat) + ggsize(650, 300) +
geomArea(stat=Stat.density(),
color="white",
tooltips=layerTooltips()
.anchor("top_right")
.format("..density..", ".4f")
.line("density|@..density..")) {x="sepal_length"; fill="species"}