The simple answer of how
William becomes
Bill is rhyming slang.
William is shortened to
Will and then rhymed to
Bill.
Robert is shortened to
Rob and then rhymed to
Bob.
Margaret has the "r" softened [think of Bostonians saying "pahk the cah"] and it becomes
Meg and that is rhymed to
Peg.