%useLatestDescriptors
%use lets-plot
%use dataframe
LetsPlot.getInfo()
Lets-Plot Kotlin API v.4.7.2. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.2.
val mpgDf = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
mpgDf.head()
DataFrame: rowsCount = 5, columnsCount = 12
untitled | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | audi | a4 | 1.800000 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 1.800000 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2.000000 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
4 | audi | a4 | 2.000000 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
5 | audi | a4 | 2.800000 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
val mpgData = mpgDf.toMap()
val pairedPalette = scaleBrewer(listOf("color", "fill"), palette = "Paired")
Stacking is the default behaviour for most area plots.
"Fill" makes it easier to compare proportions.
letsPlot(mpgData) { x = "drv"; fill = asDiscrete("year") } +
geomBar() +
pairedPalette +
ggtitle("bar: position='stack' (default)")
letsPlot(mpgData) { x = "drv"; fill = asDiscrete("year") } +
geomBar(position = positionFill()) +
pairedPalette +
ggtitle("bar: position='fill'")
letsPlot(mpgData) { x = "hwy"; fill = asDiscrete("year") } +
geomHistogram(bins = 12) +
pairedPalette +
ggtitle("histogram: position='stack' (default)")
letsPlot(mpgData) { x = "hwy"; fill = asDiscrete("year") } +
geomHistogram(bins = 12, position = positionFill()) +
pairedPalette +
ggtitle("histogram: position='fill'")
letsPlot(mpgData) { x = "hwy"; fill = asDiscrete("year") } +
geomArea(stat = Stat.density(), color = "white") +
pairedPalette +
ggtitle("area: position='stack' (default)")
letsPlot(mpgData) { x = "hwy"; fill = asDiscrete("year") } +
geomArea(stat = Stat.density(), position = positionFill(), color = "white") +
pairedPalette +
ggtitle("area: position='fill'")
Control the stacking order by changing the order
parameter of the asDiscrete()
function.
letsPlot(mpgData) { x = "hwy" } +
geomArea(stat = Stat.density(), color = "white") { fill = asDiscrete("drv") } +
pairedPalette +
ggtitle("Default order of drive types")
letsPlot(mpgData) { x = "hwy" } +
geomArea(stat = Stat.density(), color = "white") { fill = asDiscrete("drv", order = -1) } +
pairedPalette +
ggtitle("Backward order of drive types")
Let's have a look at the geometries, for which the default position adjustment differs from 'stack'
and 'fill'
.
When stacking across multiple layers it's a good idea to set the group
aesthetic - this ensures that all layers are stacked in the same way.
letsPlot(mpgData) { x = "drv"; color = asDiscrete("year") } +
geomLine(stat = Stat.count(), position = positionStack(), size = 1.5) +
geomPoint(stat = Stat.count(), position = positionStack(), size = 5) +
pairedPalette +
ggtitle("Line and point")
letsPlot(mpgData) { x = "drv" } +
geomArea(stat = Stat.count(), position = positionStack(), color = "white") { fill = asDiscrete("year") } +
geomLine(stat = Stat.count(), position = positionStack(), color = "white") { group = "year" } +
pairedPalette +
ggtitle("Area and line")
vjust
¶The vjust
argument of positionStack()
is used to move the location of plot elements vertically (vjust stands for vertical adjustment).
letsPlot(mpgData) { x = "drv" } +
geomArea(stat = Stat.count(), position = positionStack(), color = "white") { fill = asDiscrete("year") } +
geomLabel(stat = Stat.count(), position = positionStack(), color = "black") {
label = "..count.."; group = "year"
} +
pairedPalette +
ggtitle("vjust=1 (default)")
letsPlot(mpgData) { x = "drv" } +
geomArea(stat = Stat.count(), position = positionStack(), color = "white") { fill = asDiscrete("year") } +
geomLabel(stat = Stat.count(), position = positionStack(vjust = 0.5), color = "black") {
label = "..count.."; group = "year"
} +
pairedPalette +
ggtitle("vjust=0.5")
mode
¶By default only objects from different groups are stacked over each other, and objects inside one group are positioned as in position = positionIdentity
.
This behaviour could be changed by switching mode
parameter.
mode="all"
means, that each object will be shifted.
letsPlot(mpgData) { x = "hwy"; color = "class" } +
geomPoint(y = 1, position = positionStack()) +
coordFixed() + ylim(1 to 35) +
ggtitle("mode='groups' (default)")
letsPlot(mpgData) { x = "hwy"; color = "class" } +
geomPoint(y = 1, position = positionStack(mode = "all")) +
coordFixed() + ylim(1 to 35) +
ggtitle("mode='all'")
val data = mapOf(
"x" to listOf(0, 1, 2, 0, 1, 2),
"y" to listOf(4, 2, 3, 0, 1, -1),
"g" to listOf("a", "a", "a", "b", "b", "b")
)
letsPlot(data) { x = "x"; y = "y"; fill = "g" } +
geomBar(stat = Stat.identity) +
geomHLine(yintercept = 0, color = "black") +
pairedPalette +
theme(axisLine = "blank", axisTicks = "blank")
letsPlot(data) { x = "x"; y = "y" } +
geomBar(stat = Stat.identity, position = positionFill()) { fill = "g"} +
geomLabel(stat = Stat.identity, position = positionFill(vjust = 0.5)) { label = "g"; group = "g" } +
geomHLine(yintercept=0, color="black") +
pairedPalette +
theme(axisLine = "blank", axisTicks = "blank")