%useLatestDescriptors
%use dataframe
%use lets-plot
import javax.imageio.ImageIO
ImageIO.read(File("images/theme_legend_scheme.png"))
val df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
val dataMap = df.toMap()
df.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 |
Parameters to customize the legend position and justification:
legendPosition
- the position of legends;
legendJustification
- anchor point for positioning legend;
legendDirection
- layout of items in legends.
val p1 = letsPlot(dataMap) { x = "displ"; y = "hwy" } +
geomPoint(size = 5) { color = "cty"; shape = "drv" }
p1
// Draw legend below the plot area
p1 + theme().legendPositionBottom()
// Specify position, justification and orientation
p1 + theme().legendPosition(1, 1).legendJustification(1, 1).legendDirectionHorizontal()
Parameters 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()
.
val p2 = letsPlot(dataMap) { 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()
p2
legendMargin
¶p2 + theme(legendMargin = listOf(10, 20))
p2 + theme().legendBoxHorizontal()
p2 + theme().legendBoxJustificationCenter()
legendBoxSpacing
¶p2 + theme(legendBoxSpacing = 50).legendBoxHorizontal()
legendSpacing
¶p2 + theme(legendSpacing = 50).legendBoxHorizontal()
Parameters to customize the legend key:
legendKey
- background underneath legend keys, set with elementRect()
;
legendKeySize
- size of legend keys;
legendKeyWidth
- key background width;
legendKeyHeight
- key background height;
legendKeySpacing
- spacing between legend keys;
legendKeySpacingX
- spacing in the horizontal direction;
legendKeySpacingY
- spacing in the vertical direction.
val p3 = letsPlot(dataMap) { x = "hwy" } +
geomDotplot(color="pen") { fill = "class" } +
theme(legendBackground = elementRect(size = 0.5))
p3
Add background underneath legend keys:
val p4 = p3 + theme(legendKey = elementRect(fill = "#efedf5", color = "#756bb1"))
p4
Change size of legend keys:
p4 + theme(legendKeySize = 30)
val p5 = p4 + theme(legendKeyWidth = 80, legendKeyHeight = 20)
p5
Add spacing between legend keys:
p5 + theme(legendKeySpacingY = 10)