%useLatestDescriptors
%use dataframe
%use lets-plot
LetsPlot.getInfo()
Lets-Plot Kotlin API v.4.9.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.5.1.
var mpg_df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
mpg_df.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 mpg_dat = mpg_df.toMap()
val p = letsPlot(mpg_dat) + geomPoint { x="displ"; y="hwy"; color="drv" }
p
p + ggtitle("The plot title using 'ggtitle()'")
p + labs(title = "The plot title using 'labs()'")
// Add subtitle using ggtitle()
p + ggtitle("The plot title", subtitle = "The plot subtitle")
// Add subtitle using labs()
p + labs(title = "The plot title", subtitle = "The plot subtitle")
// Add caption
val p2 = p + labs(title = "The plot title", subtitle = "The plot subtitle", caption = "The plot caption")
p2
// Add color for titles
// 'title' applies to plot's title, subtitle, caption
p2 + theme(title = elementText(color="blue"))
// 'plot_title' will also apply to the subtitle
p2 + theme(plotTitle = elementText(color="blue"),
text = elementText(face="italic"),
title = elementText(face="italic"),
plotSubtitle = elementText(face="italic"),
plotCaption = elementText(face="italic"),
axisTitle = elementText(face="italic"),
legendTitle = elementText(face="italic"))
// Set own colors
p2 + theme(
plotTitle = elementText(color="blue"),
plotSubtitle = elementText(color="red"),
plotCaption = elementText(color="dark_green"))
// Multiple lines - using `\n`
p + labs(
title = "The plot title:\nFuel efficiency for most popular models of car",
subtitle = "The plot subtitle:\nPoints are colored by the type of drive train",
caption = "The plot caption:\nmpg dataset"
) + theme(plotSubtitle = elementText(color="gray"), plotCaption = elementText(color="light_gray"))
// Legend title
val p1 = letsPlot(mpg_dat) + geomPoint(size=4) { x="displ"; y="hwy"; color="cty"; shape="drv" }
p1 + labs(color="City mileage", shape="Drive type")
// Change legend position
p1 + labs(color="City mileage", shape="Drive type") + theme().legendPositionBottom()
// Use multiple lines in legend titles
p1 + labs(color="City mileage\n(mpg)",
shape="Drive type\n(front/4/rear wheel)")
p1 + theme().legendPositionBottom() +
labs(color="City mileage\n(mpg)", shape="Drive type\n(front/4/rear wheel)")
p1 + theme().legendPositionBottom() +
labs(color="City mileage\n(mpg)", shape="Drive type\n(front/4/rear wheel)") +
scaleShape(guide=guideLegend(nrow=3))