Reputation: 373
I am encountering a strange issue! I am trying to plot using groupedbar but I facing this strange issue. here is the code to generate the data and plot it:
nam = string.(repeat(1:20, outer=2))
sx = repeat(["Pre-Polarization", "Post-Polarization"], inner = 20)
c = 1:40
groupedbar(nam, c, group = sx, xlabel = "Groups", ylabel = "Scores",
title = "Scores by group and category", bar_width = 0.9,
lw = 0, framestyle = :box)
And I get the following results:
Does anybody know the reason it's happening?
Upvotes: 1
Views: 722
Reputation: 335
If strings are required, you can exploit the fact that space comes before numbers in lexicographical sort, such that " 3" < "10"
For example:
nam = (repeat(1:20, outer=2))
# Space sorts before numbers
nam = [ n >= 20 ? "$n" :
n >= 10 ? " $n" :
" $n"
for n in nam]
sx = repeat(["Pre-Polarization", "Post-Polarization"], inner = 20)
c = 1:40
groupedbar(nam, c, group = sx, xlabel = "Groups", ylabel = "Scores",
title = "Scores by group and category", bar_width = 0.9,
lw = 0, framestyle = :box)
The keen-eyed observer might notice a slight misalignment of the numbers now. This can be fixed by using either U+200B Zero Width Space or an U+2063 Invisible Seperator in place of the regular space, though it would make the code harder to read.
Upvotes: 0
Reputation: 6086
The reason the X axis values look strange is the Julia is sorting the numbers as strings, not as numbers. This means, for example, that "3" > "20" in your code for nam.
To fix this you should not stringify nam before it is plotted. So use
nam = repeat(1:20, outer=2)
in the above code.
Upvotes: 2