'count'
and 'count2d'
statistics¶geomBar()
uses 'count'
statistical transformation. In addition to '..count..'
variable, the statistics now provide additional variables related to sum ('..sum..'
), '..prop..'
(proportion) and '..proppct..'
(proportion in percent).
geomPie()
uses 'count2d'
statistical transformation. It allows to make a slice sizes proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). Also 'count2d'
provides variables for proportion ('..prop..'
) and proportion in percent ('..proppct..'
).
%useLatestDescriptors
%use lets-plot
%use dataframe
LetsPlot.getInfo()
Lets-Plot Kotlin API v.4.4.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.3.2.0.
var mpgDf = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
mpgDf.head()
DataFrame: rowsCount = 5, columnsCount = 12
val mpgData = mpgDf.toMap()
val blankTheme = theme(line = elementBlank(), axis = elementBlank())
Prepare the tooltips content to show the variables calculated by the statistics
val tooltipContent = layerTooltips()
.title("^fill")
.line("count of records in group|@{..count..} (@{..prop..})")
.line("total count of records|@{..sum..}")
.format("..prop..", ".0%")
.format("..count..", ".1f")
.format("..sum..", ".1f")
Let's show the variables computed by the in the tooltips.
ggplot(mpgData) +
geomBar(tooltips = tooltipContent) { x = "manufacturer"; fill = "class" }
Let's build a pie chart where the sector will correspond to the car class. All statistical information is shown in tooltips.
ggplot(mpgData) +
geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) { fill = "class" } +
blankTheme
Compute weighted sum instead of simple count: let's use total engine displacement of each class.
ggplot(mpgData) +
geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) {
fill = "class"
weight = "displ"
} +
blankTheme
Then let's order sectors by '..count..'
:
ggplot(mpgData) +
geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) {
fill = asDiscrete("class", orderBy = "..count..")
weight = "displ"
} +
blankTheme
Map size
to computed variable '..sum..'
:
ggplot(mpgData) +
geomPie(tooltips = tooltipContent) { fill = "class"; size = "..sum.." } +
facetWrap(facets = "manufacturer", ncol = 5, order = 1) +
scaleSize(range = 4 to 10) +
guides(size = "none") +
blankTheme