Make sure your pegs are level with each other! Use a leveler if you aren’t sure.
You can also buy stick-on adhesives for walls. Since you’re using the pegs to hang up lightweight hats, you don’t need to worry about the pegs being pulled out of the walls.
You don’t need to buy actual hatboxes if you don’t want to. Wide shoeboxes or even clear plastic boxes would also work well.
Baseball caps, cowboy hats, and derby hats would all nest well.
Stacking the boxes on top of each other is a great way to save space!