Medical doctors consistently rank among the highest compensated professions in the country. In every state, an average primary care physician earns at least $166,000 more than the average salary across all occupations. However, how much doctors earn varies greatly depending on location and specialty.

The average primary care physician’s annual salary ranges from roughly $205,000 in West Virginia to $330,000 in Alaska. In order to determine the states where doctors earn the highest and lowest salaries, 24/7 Wall St. reviewed 2014 and 2015 salary data provided by Doximity, an online social networking service for U.S. doctors.

While health insurance coverage and health care spending per capita varies widely across states, such factors do not appear to bear a strong relationship with doctors’ salaries. Rather, basic economic forces largely determine doctor salaries in each state.

In keeping with the laws of supply and demand, doctor salaries tend to be higher where there are fewer doctors. There are roughly 127 primary care physicians for every 100,000 Americans nationwide. In all but three of the 25 states where doctors earn the most, there are fewer primary care physicians per capita than there are nationwide. Conversely, there are more primary care physicians per capita than there are nationwide in a majority of the states on the lower end of the doctor pay scale.

48.  Michigan

> Avg. doctor salary: $228,750
> Avg. medical specialist salary: $341,000
> Avg. salary for all occupations: $45,140
> Primary care physicians (per 100,000 residents): 124.1 (21st highest)
> Health care spending per capita: $6,618 (19th lowest)
> Uninsured rate: 12.9% (15th lowest)

In an interview with 24/7 Wall St., Joel Davis, a spokesperson for Doximity, explained that the physical location and desirability of a state as a place to live can also play a role in a physician’s pay. Pay needs to be higher in order to “get physicians to take a job in Alaska, for instance, as compared to getting somebody to take a job in … New York or California or coastal state,” said Davis. Indeed, 19 of the states where primary care physicians earn the lowest salaries are coastal states, while the majority of states with doctors earning the highest average salaries have no oceanic or great lake coastline.

The presence of medical schools in a given state can increase the number of doctors, and consequently, affect the average doctor’s salary, Davis explained. “States with more medical schools tend to produce more doctors, and there’s a higher propensity for those doctors to stay in state.” To be sure, 18 of the 25 highest paying states for doctors are home to schools enrolling fewer medical students per capita than the national enrollment ratio. In West Virginia, the state with the lowest average pay for primary care physicians, there are 84 medical students for every 100,000 residents, the largest share in the country and more than double the corresponding national figure.

Along with geography, specialty also plays a considerable role in pay disparity among medical doctors. On average, specialists earn higher salaries than primary care physicians in every state. “Somebody who specializes has a greater degree of knowledge and skill … within a specific subdomain,” Davis said. Perhaps as a result, there are “fewer of them and they get paid more to do what they do.” Incomes still vary greatly among these higher paid positions. While the average pediatric endocrinologist earns roughly $185,000 annually, for example, neuro and thoracic surgeons each earn average salaries of well over half a million dollars.

In order to determine the states paying doctors the most and least, 24/7 Wall St. reviewed physician pay data from Doximity, an online social networking service for U.S. physicians. Doximity compiled doctor pay data from surveys of more than 35,000 doctors across the U.S. in 49 different specialties in 2014 and 2015. Average pay by state across all occupations came from the Bureau of Labor Statistics (BLS) for the most recent available year. We also considered medical school enrollment data by state from the Association of American Medical Colleges (AAMC). The number of primary care physicians per capita came from the Health Resources and Services Administration. Rates of insurance coverage were provided by County Health Rankings.

Click here to see the states where doctors earn the most (and least).