我正试图在下面的情节中添加一个传奇.该图创建了两个系列(表示概率质量函数).无论如何,我希望有一个传说,对于红色系列来说是"观察"的,对于黑色系列来说是"理论"的.下面的代码应该与来自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())