%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.
_rand = java.util.Random(32)
val mpg100 = mpg.sampleN(n = 100)
letsPlot(mpg100.toMap()) + geomYDotplot {y = "hwy"}
geomDotplot()
and geomViolin()
¶val PACIFIC_BLUE = "#118ed8"
val plotBase = letsPlot(mpg100.toMap()) {x = "drv"; y = "hwy"}
val plots = listOf(
plotBase +
geomViolin(fill = PACIFIC_BLUE, size = 0) +
geomYDotplot(binWidth = 1.6, stackRatio=.5, fill = "white", color = PACIFIC_BLUE) +
ggtitle("violin + ydotplot (method='dotdensity')"),
plotBase +
geomViolin(fill = PACIFIC_BLUE, size = 0) +
geomYDotplot(binWidth = 1.6, stackRatio=.5, method = "histodot", fill = "white", color = PACIFIC_BLUE) +
ggtitle("violin + ydotplot (method='histodot')"),
)
gggrid(plots, 2, 450, 300)
stackDir
¶val plots1 = listOf(
plotBase + geomYDotplot(binWidth = 1.5, stackDir = "left") + ggtitle("stackDir = \"left\""),
plotBase + geomYDotplot(binWidth = 1.5, stackDir = "right") + ggtitle("stackDir = \"right\""),
plotBase + geomYDotplot(binWidth = 1.5, stackDir = "center") + ggtitle("stackDir = \"center\""),
plotBase + geomYDotplot(binWidth = 1.5, stackDir = "centerwhole") + ggtitle("stackDir = \"centerwhole\""),
)
gggrid(plots1, 2, 450, 300)
stackRatio
¶val plots2 = listOf(
plotBase + geomYDotplot(binWidth = 1.5, stackRatio = 1) + ggtitle("stackRatio = 1"),
plotBase + geomYDotplot(binWidth = 1.5, stackRatio = 0.5) + ggtitle("stackRatio = 0.5"),
plotBase + geomYDotplot(binWidth = 1.5, stackRatio = 1.5) + ggtitle("stackRatio = 1.5"),
)
gggrid(plots2, 2, 450, 300)
dotSize
¶val plots3 = listOf(
plotBase + geomYDotplot(binWidth = 1.5, dotSize = 1) + ggtitle("dotSize = 1"),
plotBase + geomYDotplot(binWidth = 1.5, dotSize = 0.5) + ggtitle("dotSize = 0.5"),
plotBase + geomYDotplot(binWidth = 1.5, dotSize = 1.5) + ggtitle("dotSize = 1.5"),
)
gggrid(plots3, 2, 450, 300)
center
¶val plots4 = listOf(
plotBase + geomYDotplot(binWidth = 1.5, method = "histodot") + ggtitle("Default"),
plotBase + geomYDotplot(binWidth = 1.5, method = "histodot", center = 11) + ggtitle("center = 11"),
)
gggrid(plots4, 2, 450, 300)
boundary
¶val plots5 = listOf(
plotBase + geomYDotplot(binWidth = 1.5, method = "histodot") + ggtitle("Default"),
plotBase + geomYDotplot(binWidth = 1.5, method = "histodot", boundary = 12) + ggtitle("boundary = 12"),
)
gggrid(plots5, 2, 450, 300)
bins
¶val plots6 = listOf(
plotBase + geomYDotplot(method = "histodot") + ggtitle("Default (30)"),
plotBase + geomYDotplot(method = "histodot", bins = 15) + ggtitle("bins = 15"),
)
gggrid(plots6, 2, 450, 300)
val plots7 = listOf(
plotBase + geomYDotplot(binWidth = 1.6, stackRatio = .75, color = "white") {
fill = asDiscrete("year")
} + ggtitle("method = \"dotdensity\", stackGroups = false (default)"),
plotBase + geomYDotplot(binWidth = 1.6, stackRatio = .75, color = "white",
stackGroups = true) {
fill = asDiscrete("year")
} + ggtitle("method = \"dotdensity\", stackGroups = true"),
plotBase + geomYDotplot(binWidth = 1.6, stackRatio = .75, color = "white",
stackGroups = true, method = "histodot") {
fill = asDiscrete("year")
} + ggtitle("method = \"dotdensity\", stackGroups = true"),
)
gggrid(plots7, 1, 800, 300, fit = true)
letsPlot(mpg100.toMap()) {x = "drv"; y = "hwy"} +
geomYDotplot(tooltips = layerTooltips()
.title("Drivetrain: ^x")
.line("stack center|^y")
.line("Number of dots in stack|@..count..")
.line("Dot diameter|@..binwidth..")
)
val plotBase2 = plotBase +
scaleColorBrewer(type="qual", palette="Set1") +
scaleFillBrewer(type="qual", palette="Set1") +
themeGrey()
plotBase2 + geomYDotplot(binWidth = 1.5) {
color = asDiscrete("drv")
fill = asDiscrete("drv")
} + facetGrid(x = "year") + ggtitle("facetGrid")
coordFlip()
¶letsPlot(mpg100.toMap()) + geomYDotplot(binWidth = 1.5, stackRatio = .3, dotSize = .8, color = "white") {
x = "drv"; y = "hwy"; fill = "drv"
} + coordFlip()
val data = mapOf(
"x" to listOf(null, 'C', 'C', 'C', 'A', 'A', 'B', 'B'),
"y" to listOf(0, null, 2, 3, 0, 1, 1, 2),
"count" to listOf(2, 1, null, 3, 3, 1, 2, 0),
"binwidth" to listOf(0.25, 0.25, 0.25, null, 0.25, 0.25, 0.25, 0.25),
)
letsPlot(data) + geomYDotplot(stat = Stat.identity) {
x = "x"
y = "y"
fill = "x"
stackSize = "count"
binWidth = "binwidth"
} + ggtitle("stat = Stat.identity")
letsPlot(mpg100.toMap()) {x = "drv"; y = "hwy"} +
geomYDotplot(
method = "histodot",
bins = 14,
stackDir = "center",
stackRatio = 0.7,
stackGroups = true,
dotSize = .9,
size = .2, color = "black"
) {
fill = asDiscrete("cyl")
} +
scaleFillBrewer(palette = "Set1") +
themeGrey() + ggsize(800, 300)