Those aren't the optimal build formations imo. Though good as a general guide to what you should do, not that any new player is going to be reading this thread.
For top lane: Firstly its pretty important to have at least 1 raider above the island. It can delay a hard push by a few precious seconds when their melee goes for the bottom raider and then the top raider last, saving damage on the tower.
Secondly if you build where the front row between the towers is in that towers, from my experience you are more likely to lose docks. I put one dock on the second row in your picture to pull units instantly, and build the others behind. I also space them out, putting some melee a few spaces apart behind (but not quite staggering). This is better imo as it pulls units further back so that it takes longer for the melee to get to the towers or other units.
On the top row, imo you shouldn't build them all on that top shelf in the picture, they should go further back. This is to make sure that the pents wait long enough that they don't get aggrod by the melee (which have been pulled further inside if you build back like I recommended) but instead go straight to hitting the longboats, killing the stack faster.
For bot lane: The melee formation is definetly wrong. Building in a line like that is popular for ranged (but maybe not optimal imo, as I'll mention in a second.) and it spaces them out okay, but melee needs at least 1 more space, preferably two, in order to avoid AoE. I also like to build one in front which I want to take the aggro (almost always sword, it'll tank for my knights which i want to protect from pikemen or my pikemen which I want to protect from dying long enough to kill their knight/mounted). I also like to build one a couple of spaces back as a mini stagger, which I upgrade to the melee unit I don't want to be targeted until last. (knight or pike.)
A nice trick also is building a camp as far bottom as possible in front of the rocks. This works similarly to above top island in that the unit will often get aggroed to the back line and hit their ranged. This is extremely useful for a pike to try and kill their mounted, and its also notable that it usually doesn't get targeted straight away. It's also very effective at drawing the fire of cannons, pretty reliably doing the same job as a stagger. You can also put ranged there to achieve a similar effect, but it generally only gets targeted by 1 or 2 of their ranged so is fairly safe there. The upside of this is even better spacing for your ranged as it almost always stands on its own.
For ranged, imo it is optimal to space them out so there is two positioned above the middle of the lane, two in the middle. Ranged inevitably will clump together after a while in combat, so you need to minimise this as much as possible. This formation is less likely to clump than the line formation in my opinion.
I'll post some screenshots of my own formations later.
Also, I dread whenever my bot builds 6 mounted archers like in your screenshot. You really don't need 6, even if you manage to get them all to Dragoons (which is unlikely when you have nothing to upgrade to Musket), you still need sharpshooters later as your opponent will be getting them himself, sharpshooters being the most effective counter to dragoons. It's not even better than having 4 mounted 2 longbow (or crossbow) or 3 mounted 3 longbow (or crossbow) in the early game in my opinion. You just open yourself up to mass pike that way.