Jgafner
05/10/2024, 9:37 AMfun 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:
* @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 considerationJgafner
05/10/2024, 9:38 AMval color = listOf(
Color.PINK,
Color.PINK,
Color.PINK,
Color.PINK,
Color.PINK,
Color.PINK,
Color.ORANGE,
Color.PINK,
Color.PINK
)
Andrei Kislitsyn
05/10/2024, 9:47 AMColor
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 columnJgafner
05/10/2024, 9:48 AMJgafner
05/10/2024, 9:49 AMimport org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.geom.geomLabel
import org.jetbrains.letsPlot.ggplot
Andrei Kislitsyn
05/10/2024, 10:00 AM+ scaleFillIdentity()
to your plot.Jgafner
05/10/2024, 10:02 AMJgafner
05/10/2024, 10:02 AMAndrei Kislitsyn
05/10/2024, 10:08 AMfill="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
.Andrei Kislitsyn
05/10/2024, 10:12 AMJgafner
05/10/2024, 10:13 AM