Here's a reality not yet discussed: many doctors go into specialities to pay for medical school.
The median four-year cost of medical school (including expenses and books) was $278,455 for private schools, and $207,866 for public schools in 2013 according to the Association of American Medical Colleges.
As noted earlier, GPs or "primary care" physicians make about half as much as specialists. Imagine starting your career in debt for nearly half a million dollars (including undergraduate school). It's no wonder that compensation is a significant consideration.
This is clearly a major factor in the choice of specialties or going into primary care like general medicine or pediatrics. This has been somewhat adjusted, by increasing compensation for primary care as the demand for more primary care increased. There are still major differences in compensation, but other factors also play an important role. https://www.theatlantic.com
Most medical student are at least moderately intelligent, and have become good at gaming the system. This applies to compensation for sure, but specialization provides another benefit. A specialist masters a certain limited field, while a generalist has to try to keep up with everything in very rapidly changing medical information. Generalists deal with this by getting lots of specialty consults when there is even a suggestion of an organ specific disorder. It a typical Cover your Assets stratergery. Knowing your limits is key in this. The generalists also see many more patients/day, and the Electronic Medical Record gets more burdensome with many patients/day. Quality of life is becoming more important than compensation, after a certain baseline, that allows one to pay down the education debts. There is also a Federal program of debt forgiveness after serving definite times in a needed health care role. The future of this program is clearly uncertain in this time of chaotic changes!
This is not limited to Medicine, and you sure dont want a Corporate or International Law expert when you really need a Criminal Lawyer who has been keeping up to date in his specialized skill set.