Hi how can I feel the geomLabel with a color I cho...
# datascience
j
Hi how can I feel the geomLabel with a color I choose ? I took the example from the documentation and have this code:
Copy code
fun main() {
    val xList = listOf(
        0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0
    )
    val yList = listOf(
        0.0, 0.5, 1.0, 0.0, 0.5, 1.0, 0.0, 0.5, 1.0
    )
    val hjustMap = mapOf(0.0 to "left", 0.5 to "center", 1.0 to "right")
    val vjustMap = mapOf(0.0 to "bottom", 0.5 to "middle", 1.0 to "top")
    val hjustNames = xList.map { hjustMap[it] }
    val vjustNames = yList.map { vjustMap[it] }
    val color = listOf(
        Color.LIGHT_BLUE,
        Color.LIGHT_GREEN,
        Color.RED,
        Color.WHITE,
        Color.BLACK,
        Color.YELLOW,
        Color.ORANGE,
        Color.PINK,
        Color.GRAY
    )

    val data = mapOf(
        "x" to xList,
        "y" to yList,
        "hjust" to hjustNames,
        "vjust" to vjustNames,
        "label_nums" to xList.zip(yList).map { "${it.first}-${it.second}" },
        "label_text" to hjustNames.zip(vjustNames).map {
            "${it.first}-${it.second}"
        },
        "color" to color
    )

    val p = ggplot(data) { x = "x"; y = "y" }

    // Use name ("left", "middle", "right", "bottom", "center", "top") for hjust/vjust
    (p + geomLabel(
        size = 8, fontface = "bold", color = Color.WHITE, labelPadding = 0.5, labelR = 0.5, labelSize = 0
    ) {
        label = "label_text"; hjust = "hjust"; vjust = "vjust"; fill = "color"
    }).show()

}
now the color list is not really taken in consideration and it looks like it use the role name instead:
Copy code
* @param fill Background color of the label.
*  String in the following formats:
*  - RGB/RGBA (e.g. "rgb(0, 0, 255)")
*  - HEX (e.g. "#0000FF")
*  - color name (e.g. "red")
*  - role name ("pen", "paper" or "brush")
I tried different option like using the Color object or the toHex function but still it doesn't take those in consideration
đź‘€ 1
for instance I am getting this when running with this color list:
Copy code
val color = listOf(
    Color.PINK,
    Color.PINK,
    Color.PINK,
    Color.PINK,
    Color.PINK,
    Color.PINK,
    Color.ORANGE,
    Color.PINK,
    Color.PINK
)
a
Hi. Kandy
Color
is not compatible with the lets-plot, however you can get their hex string and use string List in lets-plot. Also here you need to add
scaleFillIdentity()
to plot which means you want to use the colors directly from the data column
j
Thanks, can you please point me to an example of this?
In fact I think I am using only lets plot in this example this is my imports:
Copy code
import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.geom.geomLabel
import org.jetbrains.letsPlot.ggplot
a
All right, then. Just add
+ scaleFillIdentity()
to your plot.
j
Yeah it is working - but I am wondering why it working like that- any documentation about it I can dive in?
*thanks
a
Basically, when you write
fill="someColumn"
you make
fill
depends on
"someColumn"
values (not assigns them, they can be any type). And there’s
scale
- a function that define how column values are mapped on fill colors (for example you have columns with “A” and “B” values and scale could be like
{"A" -> Color.RED, "B" -> Color.GREEN}
, it also can be continuous then you will have gradient scale).
scaleFillIdentity()
is a scale used when values in column are exactly values you want to assign for
fill
.
Lets-plot uses ggplot2 approach, so ggplot2 book can help you. Also, for Kandy I wrote some plotting basics in the main guide: https://kotlin.github.io/kandy/quick-start-guide.html (see “Basics” section).
j
Thanks a lot!!