I am trying to create a plot that has a line on to...
# datascience
p
I am trying to create a plot that has a line on top of a bar chart, like in the image, with a separate y axis for the bar and line charts. I tried a few different combinations of
scale
and
axis
inside
y()
without any luck, none of it worked. It seems like only the last usage of
scale
makes it into the plot.
Copy code
val wounds = listOf(4520, 3242, 3128, 3156, 4115, 5082, 5918, 3811, 5013, 6426, 5952, 5761, 5685, 5316, 4726, 4127, 4121, 3837, 3232, 2684, 2151, 1904, 1528, 1182, 971, 679, 564, 367, 276, 194, 111, 87, 59, 42, 19, 11, 1, 2)
val betterPercent = wounds.indices.map { x -> (wounds.filterIndexed { index, _ ->  index > x }.sumOf { it } / wounds.sum().toDouble() * 100.0).roundToInt() }

plot() {
    x(wounds.indices)
    bars() {
        y(wounds)
    }
    line {
        y(betterPercent){
            scale = continuous(0..100)
        }
    }
}
a
Hi! Yeah, you are right - unfortunately, Lets-Plot doesn't support multiple scales (and therefore axes) for now.
p
Is there any other way to create a plot that looks like the image I posted? For example by stacking two plots on top of each other?
a
Unfortunately, it seems that there is no workaround for this usecase in Lets-Plot. But you can get a picture like this, you need to manually scale your y values for bars into range [0, 100].
Copy code
val wounds = listOf(4520, 3242, 3128, 3156, 4115, 5082, 5918, 3811, 5013, 6426, 5952, 5761, 5685, 5316, 4726, 4127, 4121, 3837, 3232, 2684, 2151, 1904, 1528, 1182, 971, 679, 564, 367, 276, 194, 111, 87, 59, 42, 19, 11, 1, 2)
val betterPercent = wounds.indices.map { x -> (wounds.filterIndexed { index, _ ->  index > x }.sumOf { it } / wounds.sum().toDouble() * 100.0).roundToInt() }

val woundsMax = wounds.max()
val woundsScaled = wounds.map { it.toDouble() / woundsMax * 100.0 }

plot() {
    x(wounds.indices)
    bars() {
        y(woundsScaled)
    }
    line {
        y(betterPercent){
            scale = continuous(0..100)
        }
    }
}
👍 1