我正试图在下面的情节中添加一个传奇.该图创建了两个系列(表示概率质量函数).无论如何,我希望有一个传说,对于红色系列来说是"观察"的,对于黑色系列来说是"理论"的.下面的代码应该与来自Kotlin let-Plot的适当导入一起运行.我能找到的所有例子都有基于数据系列的传说.我还没有找到任何能让我深入了解如何做到这一点的例子.

    val dataMap = mapOf(
        "estimated" to listOf(0.2, 0.3, 0.4, 0.1),
        "observed" to listOf(1, 2, 3, 4),
        "probability" to listOf(0.19, 0.31, 0.42, 0.08),
        "values" to listOf(1, 2, 3, 4),
    )
    val pd = positionNudge(.1)
    var p = ggplot(dataMap) + theme().legendPositionRight() +
            geomPoint(color = "red", position = pd) {
                x = "observed"
                y = "estimated"
            } +
            geomPoint(color = "black") {
                x = "values"
                y = "probability"
            }
    for (i in 1..4) {
        p = p + geomSegment(yend = 0, color = "red", position = pd) {
            x = "observed"
            y = "estimated"
            xend = "observed"
        }
    }
    for (i in 1..4) {
        p = p + geomSegment(yend = 0, color = "black") {
            x = "values"
            y = "probability"
            xend = "values"
        }
    }
    p = p + labs(title = "some title", x = "xLabel", y = "yLabel") +
            ggsize(500, 350)

    val spec = p.toSpec()
    // Export: use PlotHtmlExport utility to generate dynamic HTML (optionally in iframe).
    val html = PlotHtmlExport.buildHtmlFromRawSpecs(
        spec, iFrame = true,
        scriptUrl = PlotHtmlHelper.scriptUrl(VersionChecker.letsPlotJsVersion)
    )
    val tmpDir = File("someDirectory")
    if (!tmpDir.exists()) {
        tmpDir.mkdir()
    }
    val file = File.createTempFile("someFileName", ".html", tmpDir)
    FileWriter(file).use {
        it.write(html)
    }
    val desktop = Desktop.getDesktop()
    desktop.browse(file.toURI())

推荐答案

您需要将数据重组为只有两个系列(红色和黑色),并添加一个分组变量:

val estimated = listOf(0.2, 0.3, 0.4, 0.1)
val observed = listOf(1, 2, 3, 4) 
val probability = listOf(0.19, 0.31, 0.42, 0.08)
val values = listOf(1, 2, 3, 4)

val data = mapOf(
    "xs" to observed + values,
    "ys" to estimated + probability,
    "group" to List(4) {"A"} + List(4) {"B"}
)

然后将" colored颜色 "美感映射到分组变量:

var p = ggplot(data) + theme().legendPositionRight() +
    geomLollipop(position = positionDodge(0.3)) {
        x = "xs"
        y = "ys"
        color = "group"
    } + scaleColorManual(values=listOf("red", "black"))
    
p = p + labs(title = "some title", x = "xLabel", y = "yLabel") +
            ggsize(500, 350)

enter image description here

Kotlin相关问答推荐

Android Jetpack编写androidx.compose.oundation.lazy.grid.Items

关键字';在When Kotlin When-语句中

Kotlin-stdlib中的模拟扩展函数

kotlin中如何使用子类调用父静态方法?

Kotlin - 如何避免在密封类的 when() 语句中转换第二个变量

Kotlin Path.useLines { } - 如何不获取 IOException("Stream closed")?

如何使用函数类型或 lambdas 作为 Kotlin 上下文接收器的类型?

为什么 Kotlin 中的 Double 和 Long 类型不推荐直接转换为 Char?

JavaFX - 你如何在 Kotlin 中使用 MapValueFactory?

如何使用 Either monad 并避免嵌套 flatMap

Kotlin:不允许在辅助构造函数参数上使用val

参考 Kotlin 中的 Java 接口静态字段

从列表中的每个对象中 Select 属性

将 Kotlin 类属性设置器作为函数引用

Kotlin 中的数据类

零安全的好处

TypeConverter()在Android的TypeConverter错误中具有私有访问权限

项目不会使用 Kotlin 1.1.3 构建

Kotlin:获取文件的扩展名,例如.txt

Kotlin反射不可用