Your picture: Yeah, it looks messed up, and it IS messed up, but it's explainable.
From what I've gathered, here's how the game picks garrison, from human perspective.
1. Pick 3 strongest ranged units, put them on wall.
2. Pick strongest melee units , place them in a good formation (spearmen/footmen front, horses back), leave room for 4 archers
3. Place archers
Now, you may think that step 2 is where it puts the spearmen. However, it always picks the highest power rating. For humans, assuming I have 1 golden squad of every unit type, spearmen are the lowest overall power of those. So because horses have a higher power rating, it will pick horses and legionnaires over the spearmen. Because horses are not frontline units, it deploys them in the back. After that, the archers get deployed and lo and behold there are some open frontline spots where it has to place them.
It's a messy way of building a garrison, I totally agree, but this would not have happened if you had 72 more legionnaires in there. It happens when you have a low number of legionnaires and a high number of horses.
So is this how it's always worked? Yeah. Is it a bug? I kind of doubt it... it's just the way it works. Either way, it's something that should definitely be changed.