%useLatestDescriptors
%use lets-plot
//%use krangl
LetsPlot.getInfo() // Work around Krangl loading old 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
val df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
df.head(3)
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 |
Shape: 3 x 12.
val p = letsPlot(df.toMap()) { x = "displ"; y = "hwy" } + geomPoint() +
labs(
title = "Fuel efficiency\nfor most popular models of car",
subtitle = "Period 1999-2008",
caption = "MPG Dataset",
x = "Engine displacement\n(litres)",
y = "Highway\nmiles per gallon"
) + themeClassic() + theme(plotBackground = elementRect(size=1))
p
// Change margins around plot title, subtitle, caption and axis titles
p + theme(plotTitle = elementText(margin = margin(t=15)),
plotSubtitle = elementText(margin = margin(b=10)),
plotCaption = elementText(margin = margin(b=15)),
axisTitleX = elementText(margin = margin(t=10)),
axisTitleY = elementText(margin = margin(r=10, l=15)))
// Change margins around axis tick labels
p + theme(axisTextX = elementText(margin = margin(t=10, b=15)),
axisTextY = elementText(margin = margin(r=10, l=15)))
// axis labels at different levels
val p2 = letsPlot(df.toMap()) { x = "class"; y = "hwy" } + geomBoxplot()
p2
p2 + theme(axisTextX = elementText(margin = margin(t=10, b=10)))
// with rotation
val p3 = p2 + ggsize(550, 400)
p3
p3 + theme(axisTextX = elementText(margin = margin(t=10)))