Although there can be debate about the details, there’s little doubt that attitudes about race and immigration played a central role in the election of Donald Trump. On survey after survey, white voters uneasy about the changing complexion of America or willing to endorse shockingly negative stereotypes about minorities went for Trump.

To predict voting for Donald Trump, ask a white person how they feel about Mexican immigrants, or whether they agree with negative stereotypes about the work ethic of black Americans. Even in the face of other plausible explanations, a strong relationship remains.

But why was this ugly underbelly of American politics so prominent in 2016? After all — as many, many people have noted — a sizable number of purportedly racist Trump voters previous voted for Barack Obama.

A key answer, my research suggests, can be found in "social geography." The phrase refers to how different groups of people, including racial and ethnic groups, are spatially arranged in the geography of America's cities, suburbs, and rural areas. The strength of racially motivated voting in 2016 can be explained in part by the types of people (white or nonwhite, immigrant or native) who lived close to Trump supporters, and, even more importantly, how this social geography had changed over time.

The resurgence of xenophobia isn’t just about the difference between Rust Belt Altoona and coastal-elite Boston, in other words. It’s also about how neighborhoods in Altoona and similar blue-collar places changed over the past 10 years.

Politics has always been about groups: us versus them, what group gets what, whether you trust people from another group, which group will govern, and whether you will support a candidate who demonizes another group. Social scientists recognized long ago that these group-based conflicts were shaped by geography.

In the early to mid-20th century, for example, they noticed how Southern white voters’ support for racist politicians seemed to be related to the nearby presence of African Americans. In the 1930s, using rudimentary statistics and data, combined with a deep ethnographic understanding, the political scientist V.O. Key argued that Southern politics “revolves around the position of the Negro.” He meant “position” literally. White Southerners in counties with large African-American populations, he found, were particularly animated to support a system of white supremacy, casting ballots for the racist candidates who supported that system.

In the past century, many of the most contentious important politics in the United States have been spurred by the movement of African Americans out of the South. In the 1940s and ’50s, when large numbers of African Americans migrated North and West, social scientists documented the resulting tension between blacks and whites in cities like Chicago and Detroit. “White flight” away from central cities stripped away much of the tax base those cities relied on, and with it many of the social services that residents needed. This, and the police brutality fueled by the white cops who stayed, led to riots and a conservative backlash in politics, starting with Richard Nixon, that still shapes politics today.

Today, we have a fuller understanding about how and why the spatial relationships between groups matters. It is the presence of a group that is proximate, yet segregated — close but far — that precipitates the politics of division. Sadly, it is when groups become close in space that they also can become politically, social, and psychologically divided, so long as also they remain segregated. This kind of relationship, the research shows, causes us to hold negative and racist attitudes about the other group: to not want to share with the other group, and, when it comes to politics, to vote against the candidate we think represents that group — or for the demagogues who attack them.

And this very situation — think a racially diverse central city and homogeneously white suburbs or an enclave full of newly arrived immigrants — is a geographic reality for most Americans.

Of course, it is not only the United States where this pattern holds: In Europe, immigration, the arrival of new groups segregated into enclaves, has become the centerpiece of many political campaigns.

You might assume that demographic change, and the proximity of different groups, would by itself breed familiarity. It turns out that’s true only in the rare cases in which those groups are integrated. When spatial patterns other than integration develop, diversity can drive us apart.

Consider how the 2016 election played out. Most white voters supported Trump and were at least willing to tolerate his xenophobic rhetoric, but his support disproportionately came from those who lived in places where the Latino population had rapidly expanded — whose social geography had changed. Many of these places experiencing a Latino boom were in states, such as Pennsylvania, crucial to Trump’s Electoral College victory. For example, in Luzerne County, Pennsylvania, where the Hispanic population grew by almost 500 percent in the last 20 years, Trump’s vote increased by over 11 percentage points from Romney in 2012, taking it from a blue to a red county.

Centre County, in Pennsylvania, had a different trajectory. Like Luzerne, it voted for Obama in 2012 and, also like Luzerne, it was more than 90 percent non-Hispanic white 20 years ago. But Centre County experienced much more modest growth in the Hispanic population; there, Trump actually performed worse than Romney had in 2012.

The psychological underpinnings of “social geography”

The intertwining of space and groups, it turns out, is deeply embedded in our psychology, in traits and habits going back to our distant evolutionary ancestors. Our minds are tuned to pay attention to geography because it was important to the evolutionary success of our ancestors who had to understand the location of things both dangerous as well as necessary for survival.

Our minds are also finely tuned to be aware of distinct groups, to treat them as shortcuts for whom to trust and with whom to cooperate. We carry this combination of awareness of groups and place with us in the modern world, and much of the ugly bigotry and racism we see today is its unfortunate legacy.

A clear example of the way we carry mental demographic maps in our minds occurs when we think about places in a city and instantly define them by ethnic population: The South Side of Chicago is black; East Los Angeles is Latino; Southie is Irish. Research shows that residents of cities have fairly accurate maps like these in their heads

When a candidate represents a group in the minds of voters, as Obama represented blacks in the eyes of white Americans, a white voter may ask herself how much she has in common with that group.

This is a hard question — the kind we are unlikely to try to answer using reason and data. Psychologists tell us our minds seek shortcuts (or a “heuristic,” in the jargon). One shortcut might be for a white person to ask herself if black people share space with people like her. (Do they live next door?) If they don’t, she might assume they are different.

We can see the effects of such mental processes: Even when holding the size of the black population constant, in 2008 and 2012, white voters who lived in areas where the black population was segregated, such as urban Hamilton County, Ohio — home of Cincinnati — or rural Hot Springs County, Arkansas, were far less likely to vote for Obama than white voters living in areas where the black population was integrated, such as Santa Clara County California.

Experimental tests of the effects of segregation on people’s attitudes

Of course, segregated and unsegregated areas often differ on many grounds besides social geography. That raises uncertainty about whether it is segregation itself that causes racial antagonism. But I and other scholars have also tested these tendencies in laboratories where conditions can be tightly controlled.

In one experiment, I invited people to a laboratory, and assigned them to meaningless groups, created by the flip of a coin, that I called “H’s” and “Y’s.” Each person knew which group she belonged to, and which group other people in the room belonged to.

I left these subjects sitting in a room for about five minutes — sometimes with H’s and Y’s integrated and sometimes with H’s and Y’s segregated (i.e., on different sides of the room). I then gave subjects money and the opportunity to anonymously share this money with a randomly chosen member from each group (the so-called “dictator game”).

It is the presence of a group that is proximate, yet segregated — close but far — that precipitates the politics of division

When the groups were segregated — even with nobody watching and no real consequences for their decision — these people shared less money across groups. They gave more to their own group, an act of pure discrimination caused by five minutes of arbitrary segregation in a laboratory, using fictional “groups.” You can just imagine what segregation can do in the real world, where it involves demographic groups about which we already have strong feelings.

There have also been so-called natural experiments — events in the real world that allow us to compare changing situations — that highlight how attitudes change when social geography changes. In Chicago, in areas like the near North Side and the former site of the Cabrini-Green housing complex, public housing created significant segregation between poor blacks and nearby whites. This extreme situation, in which groups lived close to each other, but had little contact, revealed in a powerful way the power of social geography to shape attitudes.

Even within the larger segregated city, with its fraught racial politics, the presence of the large and close-by black outgroup in these neighborhoods affected the way whites voted. Unlike other white voters in Chicago, white voters near the housing projects voted for conservative candidates and voted against Obama when he ran for US Senate. When that public housing was demolished in the early 2000s — and their black neighbors largely disappeared — these white voters stopped voting for conservative candidates.

The turnout of these white voters fell by more than 10 percentage points; the Republican vote share among these voters dropped by more 5 percentage points.

In another experiment, I sent Spanish speakers to randomly selected train stations in towns around Boston to simply catch the train and ride like any other passenger. I focused on stations in white suburbs. The intent was to create the impression, by subtle manipulation, that the Latino population in these segregated towns was increasing.

Before and after sending these Spanish speakers to the train platforms, I surveyed passengers on the platforms about their attitudes about immigration. After being exposed to the Spanish speakers on their metro lines for just three days, attitudes on these questions moved sharply rightward: The mostly liberal Democratic passengers had come to endorse immigration policies — including deportation of children of undocumented immigrants —similar to those endorsed by Trump in his campaign.

All of this adds up to something big. When Trump went to Arizona to rally his supporters and brazenly pardoned Joe Arpaio, he was stepping to a political cauldron shaped by social geography: Anglos in Maricopa County had gravitated to the racist Arpaio in response to the increasing presence of immigrants from Mexico, who were close by and yet segregated into large immigrant ghettos in the Phoenix area, which has seen the Latino population grow by more than 700 percent in the past 40 years. The metro area now is more than 40 percent Latino.

This pattern repeats itself across the United States, including in places like the communities in Pennsylvania that swung to Trump in 2016. And the pattern repeats itself across time. Just as the relatively sudden arrival of African Americans from the South in the past century swung previously Democratic white voters in Northern cities — in a segregated context — to vote for racist demagogues, including George Wallace in 1968 (and the racist, if less openly so, Nixon), suburban and rural white voters today are shaken by shifting demographic geography. They peer into diverse cities — segregated from the suburbs, internally segregated, and featuring growing numbers of Latinos, blacks, and Muslims — and embrace the politics of backlash and resentment.

Will this be an enduring pattern of American politics? One antidote to segregation is contact across groups, social scientists have long recognized. When people from different backgrounds are allowed to meet and know each other as individuals, antagonism drops.

In the United States we approach integration only obliquely and partially, allowing it to happen a bit in universities and a few kinds of workplaces, even as few neighborhoods are affected. Even our schools, once the flashpoint of efforts to desegregate, have fallen into, or remain in, patterns of separation by race and ethnicity. As long as these sweeping patterns of segregation exist, spatial geography will drive a political wedge between groups.

Of course, contact and intermingling has happened in a few places. In California when I was growing up, Latino immigration roiled the politics of the state, and politicians like Pete Wilson rode anti-immigrant rhetoric to victory. To somebody observing liberal politics today in California, this is unthinkable — largely because of integration. Yes, many Anglos left the state, but many also remained and became more comfortable with their Latino neighbors. As the groups intermixed, whites replaced the negative stereotypes induced by the old social geography with images reflecting the new ones: Latinos were their friends, neighbors, their kids’ classmates.

That kind of integration is sometimes viewed as inevitable in the long run. It’s not. For instance, we’re now seeing fresh patterns of “reverse” segregation, in which poor, minority groups congregate in suburbs as whites move into thriving city centers. These new patterns could simply replace the old patterns of segregation, reinforcing intergroup tensions.

If certain city neighborhoods and exclusive suburbs become places where only certain classes, or races, can live, then the patterns of social geography that drive us apart — and that drove some Americans toward Trump — will persist. What’s needed is a housing policy that allows for a broader mix of people to share every given neighborhood. Only then will we see the power of social geography to bring us together — not just drive us apart.

Ryan D. Enos, an associate professor of government at Harvard, is the author of the new book The Space Between Us: Social Geography and Politics. Find him on Twitter @RyanDEnos.

The Big Idea is Vox’s home for smart discussion of the most important issues and ideas in politics, science, and culture — typically by outside contributors. If you have an idea for a piece, pitch us at thebigidea@vox.com.