Vindaar / ggplotnim

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

Assertion failure for continuous color scales for some plots #100

Closed Vindaar closed 3 years ago

Vindaar commented 3 years ago

Another issue discovered in the discussion with @haxscramper.

A plot with geom_bar values, which are filled by a continuous scale can cause a runtime error due to a failed assertion in the filling of the color scale.

For the following csv file:

w1        w2        w3        w4
 4         0         0         2
39         1         5         2
11         1         0         2

the code:

import ggplotnim

var df = toDf(readCsv("data.csv", sep = ' '))
df = df.gather(df.getKeys(), "Types", "Values")
ggplot(df, aes("Types", fill = "Values")) +
  geom_bar() + ggsave("/tmp/img1.png")

results in:

home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim.nim(2656) +
/home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim.nim(2644) ggsave
/home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim.nim(2552) ggcreate
/home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim/collect_and_fill.nim(622) collectScales
/home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim/postprocess_scales.nim(643) postProcessScales
/home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim/postprocess_scales.nim(584) filledCountGeom
/home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim/postprocess_scales.nim(196) applyContScaleIfAny
/home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim/collect_and_fill.nim(260) :anonymous
/home/basti/src/nim/nim_git_repo/lib/system/assertions.nim(30) failedAssertImpl
/home/basti/src/nim/nim_git_repo/lib/system/assertions.nim(23) raiseAssert
/home/basti/src/nim/nim_git_repo/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: /home/basti/CastData/ExternCode/ggplotnim/src/ggplotnim/collect_and_fill.nim(260, 16) `t.size == df.len` Resulting tensor size does not match df len! [AssertionDefect]

The current master HEAD has slightly different lines. The offending line is: https://github.com/Vindaar/ggplotnim/blob/master/src/ggplotnim/collect_and_fill.nim#L289-L291

Investigate why the evaluation of the formula yields an empty tensor!

Vindaar commented 3 years ago

The original problem has been fixed by now.

The plot is still not being produced, because of a different issue. Essentially the code now tries to apply a continuous transformation in calling applyContScaleIfAny here:

https://github.com/Vindaar/ggplotnim/blob/master/src/ggplotnim/postprocess_scales.nim#L707

but the continuous scale Values does not exist in the DF anymore, as we called count before:

https://github.com/Vindaar/ggplotnim/blob/master/src/ggplotnim/postprocess_scales.nim#L703

count reduces the DF and thus drops additional columns. Because of that the formula evaluation happening in the aforementioned proc, creates a constant string column of values "Values". And these cannot be mapped to a continuous value, so we crash here:

https://github.com/Vindaar/ggplotnim/blob/master/src/ggplotnim/collect_and_fill.nim#L227

as we require (obviously) a constant column to be mappable to float.

The underlying problem is nowadays that the plot we ask for isn't well defined. In a bar plot what is continuous coloring supposed to mean? Even ggplot2 produces the following plot:

> df2
# A tibble: 12 x 2
   Type  Values
   <chr>  <dbl>
 1 w1         4
 2 w1        39
 3 w1        11
 4 w2         0
 5 w2         1
 6 w2         1
 7 w3         0
 8 w3         5
 9 w3         0
10 w4         2
11 w4         2
12 w4         2
> ggplot(df2, aes(Type, fill = Values)) + geom_bar()

image

Hmm.