I noticed a problem with line plots and found that it can be easily solved by pre-sorting data used for X axis. Since no other package or plotting library (like R base or ggplot2) asks me to sort data before plotting, I think it should be done automatically also in csvtk.
The solution is to just sort the data on X axis before plotting:
csvtk -H sort -k 1:n csvtk_plot_xorder_bug.csv | csvtk -H plot line -x 1 -y 2 > csvtk_plot_xorder_xsorted.png
And the result:
This is much nicer and should be done by default whenever we want to make line plots, just like R or ggplot2 do.
notes
Originally, I came across the issue when making real plots such as this one:
Only much later I found sorting the data fixes the issue, which was not obvious, so I instead had to resort to scatterplots, which are not always good fit.
I noticed a problem with line plots and found that it can be easily solved by pre-sorting data used for X axis. Since no other package or plotting library (like R
base
orggplot2
) asks me to sort data before plotting, I think it should be done automatically also incsvtk
.reproducible examples
First, let's generate some example data:
Now, we can try some plots.
wrong plots
Using the following code:
we get this plot:
That's obviously wrong. But it still happens to be sorted by the Y axis - we can mess it up even more with random sort using the following code:
good plot
The solution is to just sort the data on X axis before plotting:
And the result:
This is much nicer and should be done by default whenever we want to make line plots, just like R or
ggplot2
do.notes
Originally, I came across the issue when making real plots such as this one:
Only much later I found sorting the data fixes the issue, which was not obvious, so I instead had to resort to scatterplots, which are not always good fit.