New parameters in theme() to customize the legend spacing and margins:
legendBoxSpacing
- spacing between plotting area and legend box;
legendMargin
- margin around each legend;
legendSpacing
- spacing between legends, legendSpacingX/legendSpacingY
- in the horizontal/vertical direction.
Arrangement of multiple legends:
fun legendBoxHorizontal()
fun legendBoxVertical()
Justification of each legend within the overall bounding box:
fun legendBoxJustificationLeft()
fun legendBoxJustificationRight()
fun legendBoxJustificationTop()
fun legendBoxJustificationBottom()
fun legendBoxJustificationCenter()
%useLatestDescriptors
%use lets-plot
%use dataframe
LetsPlot.getInfo()
Lets-Plot Kotlin API v.4.9.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.5.1.
import javax.imageio.ImageIO
import java.net.URL
val url = URL("https://github.com/JetBrains/lets-plot/raw/master/docs/f-24g/images/theme_legend_scheme.png")
ImageIO.read(url)
val mpg = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg.head(3)
DataFrame: rowsCount = 3, 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 |
val p = letsPlot(mpg.toMap()) { x = "displ"; y = "cty" } +
geomPoint(shape = 21) { fill = "drv"; size = "hwy" } +
scaleSize(range = 1 to 10, breaks = listOf(15, 30, 40)) +
theme(legendBackground = elementRect(size = 1)).legendPositionBottom()
p
legendMargin
¶p + theme(legendMargin = listOf(10, 20))
p + theme().legendBoxHorizontal()
p + theme().legendBoxJustificationCenter()
legendBoxSpacing
¶p + theme(legendBoxSpacing = 50).legendBoxHorizontal()
legendSpacing
¶p + theme(legendSpacing = 50).legendBoxHorizontal()