首页 > 解决方案 > 在 Jetbrains 的 Lets-Plot 中添加字幕、标题和图例标题

问题描述

我使用https://github.com/JetBrains/lets-plot库在 Kotlin 中编写了一个绘图。我希望能够设置标题、副标题、标题,并编辑(或删除)图例标题。

    val goodColors = listOf("#A1DBB2", "#69D2E7")
    val badColors = listOf("#F45D4C", "#F7A541", "#FACA66", "#F45D4C", "#F7A541", "#FACA66")

    val barWidth = 0.4

    return letsPlot() +
            labs(x = "", y = "", title = "My title") +

            geomBar(stat = Stat.identity, position = Pos.stack, color = "black", width = barWidth) {
                x = listOf(1.0, 1.0, 2.0, 2.0).map { it - (barWidth / 2) + barWidth }
                y = listOf(16, 34, 18, 37)
                fill = listOf("Good one", "Good two", "Good one", "Good two")
            } +

            geomBar(stat = Stat.identity, position = Pos.stack, color = "red", width = barWidth) {
                x = listOf(1.0, 1.0, 1.0, 2.0, 2.0, 2.0).map { it - (barWidth / 2) }
                y = listOf(71, 20, 17, 55, 11, 10)
                fill = listOf("Bad one", "Bad two", "Bad Three", "Bad one", "Bad two", "Bad Three")
            } +

            scaleFillManual(values = goodColors + badColors) +
            scaleXDiscrete(breaks = listOf(1.0, 2.0), labels = listOf("2021-01-01", "2021-02-01")) +
            theme()

在 ggplot2 中,我可以在或中设置字幕labs(),但在 Lets-plot 中,这些函数既不接受字幕也不接受字幕。ggtitle()theme()

有没有办法设置副标题、标题以及编辑或删除图例标题?

上面代码的示例输出

标签: rkotlinggplot2lets-plot

解决方案


尝试通过为fill比例设置一个空名称来删除图例标题:

+ scale_fill_discrete(name="")

字幕和字幕的情况不好,请关注#417


推荐阅读