Units get very frustrated when being mingled with other units, or even when being very close. Also, being too close to the border, or being too close to water might make 'em do weird things.
Usually you can just order them to get back where they were, and they'll wait a few minutes before they start walking away again.
If you want them to stay in position all the time, you have to properly separate them. :)

Good luck!