Vindaar / ggplotnim

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

y axis reverse without using scale_y_reverse() in some cases #91

Closed hffqyd closed 3 years ago

hffqyd commented 3 years ago

Sorry to bother you again, but I encountered a problem when using geom_line() with some data, and the case was the same using geom_point(), as in the code showed below.

import ggplotnim

var x = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
var y = [0.0, 0.0001, 0.0, 0.0, 0.0001, 0.0003, 0.0003, 0.0001, 0.0001, 0.0003, 0.0001, 0.0003, 0.0003, 0.0001, 0.0, 0.0003, 0.0001, 0.0, 0.0004, 0.0001, 0.0015, 0.0001, 0.0001, 0.0001, 0.0, 0.0003, 0.0]

let df = seqsToDf(x, y)

ggplot(df, aes("x", "y")) +
geom_line() +
ggsave("gg.png")

And the resulting plot would be:

gg

However, sometimes with simillar data, for example, when deleted the last number in both x and y, the y axis would be not reversed.

gg2

Where was I wrong? Or if the data type was not right?

hffqyd commented 3 years ago

Hi,I'm writing to say that I happened to find a solution for this issue, while I still don't know why exactly.

I found adding scale_y_continuous() as below would fix this.

import ggplotnim

var x = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
var y = [0.0, 0.0001, 0.0, 0.0, 0.0001, 0.0003, 0.0003, 0.0001, 0.0001, 0.0003, 0.0001, 0.0003, 0.0003, 0.0001, 0.0, 0.0003, 0.0001, 0.0, 0.0004, 0.0001, 0.0015, 0.0001, 0.0001, 0.0001, 0.0, 0.0003, 0.0]

let df = seqsToDf(x, y)

ggplot(df, aes("x", "y")) +
geom_line() +
scale_y_continuous() +
ggsave("gg.png")

While changinig scale_y_continuous() to scale_y_discrete(), it will just like before. It seems at some times the data was treated as discrete, is it?

Thank you for the great package. Wish it will be something like ggplot2 in R.

Vindaar commented 3 years ago

Hey! Sorry for not answering. I was on vacation without reception. :)

While changinig scale_y_continuous() to scale_y_discrete(), it will just like before. It seems at some times the data was treated as discrete, is it?

Yes, that is exactly what seems to be happening. I'm using some simple heuristics to automatically determine whether data looks continuous or discrete. The logic isn't that great though and it's problematic, because the user does not realize it's happening. I suppose I could echo an info line for the atypical cases, e.g. if float data is interpreted as discrete.

I'll leave this issue open for sure, since that's an important thing to fix.

Vindaar commented 3 years ago

This has been fixed in #118 by changing how floating point values are handled: they are considered continuous by default now.