%useLatestDescriptors %use lets-plot //%use krangl LetsPlot.getInfo() %use krangl val mpg_df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv") mpg_df.head() val mpg = mpg_df.toMap() // Default legend val p = letsPlot(mpg) {x="displ"; y="hwy"; color="manufacturer"} + geomPoint(size=5.0) + ggsize(600,250) p // Layout the legend in two columns p + scaleColorDiscrete(guide=guideLegend(ncol=2)) + ggtitle("Two columns legend") // Same and fill by rows p + scaleColorDiscrete(guide=guideLegend(ncol=2, byRow=true)) + ggtitle("Two columns legend filled by rows") // Adjust legend and axis // - five rows legend // - draw legend below plot // - remove axis p + scaleColorDiscrete(guide=guideLegend(nrow=5)) + theme(axisTitle="blank").legendPositionBottom() + ggtitle("Five rows legend and below") + ggsize(700,400) // Set color to city MPG and shape to driveĀ·train val p1 = letsPlot(mpg) {x="displ"; y="hwy"} + geomPoint(size=5.0) {color="cty"; shape="drv"} + ggsize(700, 350) p1 // Change legend position and orientation p1 + theme().legendPosition(1, 1).legendJustification(1, 1).legendDirectionHorizontal() // Adjust colorbar size // Change order in drive-train legend // Set labels to legends and axis // Remove vertical axis line p1 + theme().legendPosition(1, 1).legendJustification(1, 1).legendDirectionHorizontal().axisLineYBlank() + scaleColorContinuous(name="City MPG", low="dark_blue", high="light_blue", guide=guideColorbar(barHeight=10, barWidth=300)) + scaleShape(name="Drive-train", breaks=listOf('f', 'r', '4'), labels=listOf("front", "rear", "4X4")) + xlab("Engine displacement (L)") + ylab("Highway MPG")