Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

No, it is not impossible to generate a legend in lets-plot-kotlin. In fact, lets-plot-kotlin supports generating legends for various aesthetics such as color, fill, size, shape, linetype, and alpha.

For example, to create a scatter plot with color scaled by a factor variable "Species" and a legend, you can write the following code:

import letsplot.*
import kotlin.random.Random

fun main() {
    // generate some random data
    val n = 100
    val species = List(n) { "Species${Random.nextInt(1, 4)}" }
    val x = List(n) { Random.nextDouble() }
    val y = List(n) { Random.nextDouble() }
    val data = mapOf("Species" to species, "x" to x, "y" to y)

    // plot the data with a legend
    val p = letsPlot(data) { x = "x"; y = "y" } +
            geomPoint(aes(color = "Species")) +
            scaleColorDiscrete() +
            labs(title = "Scatter plot with legend", x = "x", y = "y", color = "Species")

    p.show()
}

This will produce a scatter plot with a color legend titled "Species". You can customize the appearance of the legend by modifying the scaleColorDiscrete function arguments. For example, you can change the title of the legend or the position of the legend using the name and guide parameters, respectively.