%useLatestDescriptors
%use lets-plot
%use dataframe
LetsPlot.getInfo()
Lets-Plot Kotlin API v.4.4.2. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.0.0.
var mpg_df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
mpg_df.head()
DataFrame: rowsCount = 5, columnsCount = 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()
DataFrame: rowsCount = 5, columnsCount = 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"}