%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.
val mpg = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
mpg.head(3)
DataFrame: rowsCount = 3, columnsCount = 12
val p = letsPlot(mpg.toMap()) { y = "hwy" } + theme(axisTitleX="blank")
val p1 = p + ggtitle("default") + geomBoxplot{ x = "class" }
val p2 = p + ggtitle("alphabetically") + geomBoxplot{ x = asDiscrete("class", order = 1) }
val p3 = p + ggtitle("by 'middle' ↑") + geomBoxplot{ x = asDiscrete("class", orderBy = "..middle..", order = 1) }
val p4 = p + ggtitle("by 'middle' ↓") + geomBoxplot{ x = asDiscrete("class", orderBy = "..middle..", order = -1) }
val p5 = p + ggtitle("by 'ymax' ↑") + geomBoxplot{ x = asDiscrete("class", orderBy = "..ymax..", order = 1) }
val p6 = p + ggtitle("by 'ymax' ↓") + geomBoxplot{ x = asDiscrete("class", orderBy = "..ymax..", order = -1) }
val w = 470
val h = 300
val bunch = GGBunch()
bunch.addPlot(p1, 0, 0, w, h)
bunch.addPlot(p2, w, 0, w, h)
bunch.addPlot(p3, 0, h, w, h)
bunch.addPlot(p4, w, h, w, h)
bunch.addPlot(p5, 0, h*2, w, h)
bunch.addPlot(p6, w, h*2, w, h)
bunch
val classOrdered = asDiscrete("class", order=1)
val p10 = p + ggtitle("x='class', color='class'") +
geomBoxplot{ x = "class"; color = "class" }
val p11 = p + ggtitle("x=asDiscrete('class', order=1), color='class'") +
geomBoxplot{ x = classOrdered; color = "class" }
val p12 = p + ggtitle("x=asDiscrete('class', order=1),\ncolor=asDiscrete('class')") +
geomBoxplot{ x = classOrdered; color = asDiscrete("class") }
val p13 = p + ggtitle("x='class', color='drv'") +
geomBoxplot{ x = "class"; color = "drv" }
val p14 = p + ggtitle("x=asDiscrete('class', order=1),\ncolor=asDiscrete('drv', order=1)") +
geomBoxplot{ x = classOrdered; color = asDiscrete("drv", order=1) }
val p15 = p + ggtitle("x=asDiscrete('class', orderBy='..middle..')") +
geomBoxplot{ x = asDiscrete("class", orderBy="..middle.."); color = "drv" }
val bunch1 = GGBunch()
bunch1.addPlot(p10, 0, 0, w, h)
bunch1.addPlot(p11, 0, h, w, h)
bunch1.addPlot(p12, w, h, w, h)
bunch1.addPlot(p13, 0, h*2, w, h)
bunch1.addPlot(p14, 0, h*3, w, h)
bunch1.addPlot(p15, w, h*3, w, h)
bunch1
val p21 = p +
geomCrossbar(stat = Stat.boxplot()) { x = "class"; color = "class"} +
scaleColorBrewer(palette = "Dark2") +
ggtitle("default")
val p22 = p +
geomCrossbar(stat = Stat.boxplot()) {
x = asDiscrete("class", orderBy = "..middle..", order = 1);
color = "class"} +
scaleColorBrewer(palette = "Dark2") +
ggtitle("order by 'middle' ↑, also applies to 'color'")
val p23 = p +
geomCrossbar(stat = Stat.boxplot()) {
x = asDiscrete("class", orderBy = "..middle..");
color = asDiscrete("class", order = 1)} +
scaleColorBrewer(palette = "Dark2") +
ggtitle("'class' and 'orderBy' + 'class' and 'order':\noptions are combined")
val bunch2 = GGBunch()
bunch2.addPlot(p21, 0, 0, w, h)
bunch2.addPlot(p22, w, 0, w, h)
bunch2.addPlot(p23, 0, h, w, h)
bunch2
val p31 = p + ggtitle("default") + geomBar { x = "class" }
val p32 = p + ggtitle("alphabetically") + geomBar { x = asDiscrete("class", order = 1) }
val p33 = p + ggtitle("by 'hwy' ↓") + geomBar { x = asDiscrete("class", orderBy = "hwy") }
val p34 = p + ggtitle("by 'hwy' ↑") + geomBar { x = asDiscrete("class", orderBy = "hwy", order = 1) }
val bunch3 = GGBunch()
bunch3.addPlot(p31, 0, 0, w, h)
bunch3.addPlot(p32, w, 0, w, h)
bunch3.addPlot(p33, 0, h, w, h)
bunch3.addPlot(p34, w, h, w, h)
bunch3
val p41 = p + ggtitle("default") + geomBar { x = "manufacturer"; fill = "class" }
val p42 = p + ggtitle("'x' alphabetically") + geomBar { x = asDiscrete("manufacturer", order = 1); fill = "class" }
val p43 = p + ggtitle("'x' alphabetically + samplingPick(4)") +
geomBar(sampling = samplingPick(4)) {
x = asDiscrete("manufacturer", order = 1);
fill = "class"
}
val p44 = p + ggtitle("'fill' alphabetically") + geomBar { x = "manufacturer"; fill = asDiscrete("class", order=1) }
val p45 = p + ggtitle("'x' & 'fill' alphabetically") +
geomBar { x = asDiscrete("manufacturer", order=1); fill = asDiscrete("class", order=1) }
val p46 = p + ggtitle("'x' by count ('sum' aggregation)") +
geomBar { x = asDiscrete("manufacturer", orderBy="..count.."); fill = "class" }
val p47 = p + ggtitle("'x' by count + samplingPick(4)") +
geomBar(sampling = samplingPick(4)) {
x = asDiscrete("manufacturer", orderBy="..count..");
fill = "class"
}
val bunch4 = GGBunch()
bunch4.addPlot(p41, 0, 0, w, h)
bunch4.addPlot(p42, 0, h, w, h)
bunch4.addPlot(p43, w, h, w, h)
bunch4.addPlot(p44, 0, h*2, w, h)
bunch4.addPlot(p45, w, h*2, w, h)
bunch4.addPlot(p46, 0, h*3, w, h)
bunch4.addPlot(p47, w, h*3, w, h)
bunch4
val pp = letsPlot(mpg.toMap()) { x = "fl"; fill = "drv" }
val p51 = pp + geomBar() + ggtitle("Default")
val p52 = pp + geomBar(position=positionDodge()) + ggtitle("With Dodge Position")
val pOrder1 = ggplot(mpg.toMap()) { x = asDiscrete("fl", order=1); fill = asDiscrete("drv", order=1) }
val title1 = ggtitle("'x' and 'fill' alphabetically")
val p53 = pOrder1 + geomBar() + title1
val p54 = pOrder1 + geomBar(position=positionDodge()) + title1
val pOrder2 = ggplot(mpg.toMap()) { x = asDiscrete("fl", orderBy="..count.."); fill = "drv" }
val title2 = ggtitle("'x' by 'count'")
val p55 = pOrder2 + geomBar() + title2
val p56 = pOrder2 + geomBar(position=positionDodge()) + title2
val bunch5 = GGBunch()
bunch5.addPlot(p51, 0, 0, w, h)
bunch5.addPlot(p52, w, 0, w, h)
bunch5.addPlot(p53, 0, h, w, h)
bunch5.addPlot(p54, w, h, w, h)
bunch5.addPlot(p55, 0, h*2, w, h)
bunch5.addPlot(p56, w, h*2, w, h)
bunch5
// diamonds
val diamonds = DataFrame.readCSV("https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/diamonds.csv")
diamonds.head(3)
DataFrame: rowsCount = 3, columnsCount = 11
val pd = letsPlot(diamonds.toMap())
val p61 = pd + geomBar { x = "cut"; fill = "clarity" } + ggtitle("Default")
val p62 = pd + geomBar(position=positionFill()) { x = "cut"; fill = "clarity" } + ggtitle("position='fill'")
val p63 = pd + ggtitle("'x' and 'fill' alphabetically") +
geomBar(position=positionFill()) { x = asDiscrete("cut", order=1);
fill = asDiscrete("clarity", order=1) }
val p64 = pd + ggtitle("'x' order by 'count'") +
geomBar(position=positionFill()) { x = asDiscrete("cut", orderBy="..count..");
fill = asDiscrete("clarity", order=1) }
val p65 = pd + geomBar(position=positionDodge()) { x = "cut"; fill = "clarity" } + ggtitle("position='dodge'")
val p66 = pd + ggtitle("'x' order by 'count' and 'fill' - alphabetically") +
geomBar(position=positionDodge()) { x = asDiscrete("cut", orderBy="..count..");
fill = asDiscrete("clarity", order=1) }
val bunch6 = GGBunch()
bunch6.addPlot(p61, 0, 0, w, h)
bunch6.addPlot(p62, w, 0, w, h)
bunch6.addPlot(p63, 0, h, w, h)
bunch6.addPlot(p64, w, h, w, h)
bunch6.addPlot(p65, 0, h*2, w, h)
bunch6.addPlot(p66, w, h*2, w, h)
bunch6