Vindaar / ggplotnim

A port of ggplot2 for Nim
https://vindaar.github.io/ggplotnim
MIT License
176 stars 15 forks source link

Range computation broken for stacked histogram with stat = identity #99

Closed Vindaar closed 3 years ago

Vindaar commented 3 years ago

Noticed this helping out @haxscramper on discord.

The code below produces a plot with the y range not properly computed. For the stat = "identity" call we end up in the filledIdentityGeom: https://github.com/Vindaar/ggplotnim/blob/master/src/ggplotnim/postprocess_scales.nim#L345 which does not have an equivalent of addBinCountsByPosition or addCountsByPosition: https://github.com/Vindaar/ggplotnim/blob/master/src/ggplotnim/postprocess_scales.nim#L219-L250

import ggplotnim, random, sequtils

type
  MonthdayRange = range[0 .. 31]
  Word = enum
    w1, w2, w3, w4
  Data = array[Word, array[MonthdayRange, int]]

randomize()
var data: Data
for w in Word:
  var dInner: array[MonthdayRange, int]
  for day in 0 ..< MonthdayRange.high:
    dInner[day] = rand(40)
  data[w] = dInner

var df = newDataFrame()
for w in Word:
  var dfLoc = seqsToDf({ "Monthday" : toSeq(MonthdayRange.low .. MonthdayRange.high).mapIt(it.int),
                         "Value" : toSeq(data[w]) })
  dfLoc["Word"] = constantColumn($w, dfLoc.len)
  df.add dfLoc
  echo df

ggplot(df, aes("Monthday", "Value", fill = "Word")) +
  geom_histogram(stat = "identity") +
  ylim(0, 120) +
  ggsave("image-1.png")

image-1

what it should look like:

image-1

Vindaar commented 3 years ago

Fixed by #119.