JetBrains / lets-plot-kotlin

Grammar of Graphics for Kotlin
https://lets-plot.org/kotlin/
MIT License
422 stars 36 forks source link

Error: Variable not found in data frame #103

Closed PoslavskySV closed 2 years ago

PoslavskySV commented 2 years ago

Dear Colleagues,

the following grouped box plot example does not work when trying to add text labels to groups:

%useLatestDescriptors
%use lets-plot
%use dataframe

val data = DataFrame.readCSV("https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/datasets/ToothGrowth.csv")

var plt = letsPlot(data.toMap()) {
    x = "dose"
    y = "len"
    group = "supp"
}
plt += geomBoxplot { fill = "supp" }
plt += geomText(
    mapOf(
        "dose" to listOf(0.5, 1.0, 2.0),
        "ll" to listOf("A", "B", "C"),
        // "supp" to listOf(null, null , null)   // <-- only works when this line is uncommented
    ),
    y = 35.0
) {
    x = "dose"
    label = "ll"
}
plt

The error is:

Variable not found: 'supp'. Variables in data frame: ['ll', 'transform.LABEL', 'dose', 'transform.X']

The workaround is to add a "fake" supp series in geomText. Isn't it a bug?

alshan commented 2 years ago

Hi, I don't think it's a bug. The 'text' layer is looking for the 'supp' variable because you have specified mapping group = "supp" in a block which is applied to the entire plot (all layers).

In this example you can just remove mapping group = "supp" altogether because geomBoxplot { fill = "supp" } will do grouping in the 'boxplot' layer anyways.

You can also dispense with x = "dose" mapping in the 'text' layer because this mapping is already defined earlier.

PoslavskySV commented 2 years ago

Understood. Thank you!