For President Obama, re-election proved to be a more nerve-racking ride than his election. Four years ago, aided by an unpopular Republican incumbent, a financial crisis and a wave of enthusiasm, Mr. Obama defeated John McCain by seven percentage points in the national popular vote.

His fight for a second term was more of a slog. The economic recovery was steady but tepid, and while some states are still tallying votes, Mr. Obama leads former Mitt Romney by just under three percentage points nationally.

In 46 states and the District of Columbia, President Obama did worse in 2012 than he did in 2008, winning by less or losing by more. The vote in most counties, too, shifted to the political right.

But separating out the national political environment from more fundamental and potentially longer-lasting political shifts at the state level is harder. Relative to the national popular vote, the picture is muddled: 29 states and the District of Columbia shifted toward Mr. Obama, and 21 states shifted toward the Republican Party. But the partisan lean in most states moved only slightly, and only one state flipped from leaning toward one party to the other.

State-Level Margins Relative to the Nation

(Ordered by Shift in Relative Vote)

Some of the most emphatic shifts are explained by home-state effects. Alaska, where the 2008 Republican vice-presidential nominee Sarah Palin served as governor, was 12 percentage points less Republican-leaning this year. In Utah, where a majority of residents share the Mormon faith with Mr. Romney, the vote was 15 percentage points more Republican-leaning in 2012.

Many of the states that, relative to the nation, moved toward the Democratic Party have large minority populations, including New Jersey, Louisiana, Mississippi, New York and Maryland.

Non-demographic factors may have also shifted the vote in certain states. Two of the states that moved furthest to the left bore the brunt of Hurricane Sandy, New Jersey and New York. There is the possibility that Sandy (and Gov. Chris Christie’s effusive praise of Mr. Obama on the president’s trip to see the damage in New Jersey) had an effect. Exit polls showed that Mr. Obama’s highest levels of support among Republicans were in New York and New Jersey.

The 2012 battleground states hardly shifted at all relative to the national popular vote. Wisconsin, home to vice-presidential nominee Paul Ryan, moved furthest right, shifting by three percentage points toward the Republicans.

Florida moved furthest left. In 2008, Mr. Obama carried Florida, but the state was 4.5 percentage points more Republican-leaning than the nation. If the Sunshine State’s relative lean had remained around 4 percent, Mr. Romney would have carried the state in 2012. But Florida’s fast-growing, non-Cuban Hispanic population appears to have given Mr. Obama a boost. Florida is still a red-leaning state, but by under two percentage points now.

At the other end of the spectrum, Mr. Romney substantially over-performed Mr. McCain in Indiana and Missouri. Both were battleground states in 2008 but were largely uncontested in 2012. Left alone by the campaigns, Indiana and Missouri seem to have reverted to their normal Republican lean.

West Virginia, too, seems to have moved even further away from the Democratic Party at the presidential level. And Mr. Romney also far outperformed Mr. McCain in the northern Great Plains states, North Dakota, South Dakota, Montana and Wyoming. Although, it appears Mr. Romney’s coattails were short, as Democrats won surprising victories in Senate races in North Dakota and Montana.

But over all, when the swing in the national political environment is subtracted out, most states didn’t shift that far left or right. Twenty-six states moved by two percentage points or less. Arranged by the margin of victory relative to the national popular vote, the states — with a few exceptions — fall in the same order as they did in 2008.

State-Level Margins Relative to the Nation

(Ordered by 2012 Lean)

Colorado — the tipping point state in 2008 — barely budged relative to the nation and so played the role of tipping point again in 2012.

Virginia also barely moved. It was just one percentage point more left-leaning in 2012 relative to the country.

However, Virginia’s small shift happened to be across the nation’s partisan dividing line. Relative to the national popular vote, Virginia was the only state to flip from Republican-leaning in 2008 to Democratic-leaning in 2012. No state flipped from Democratic-leaning to Republican-leaning.

By the count as of now, Mr. Obama won the national popular vote by 2.8 percentage points and carried Virginia by 3.1 percentage points, giving Virginia, at least for now, a slim Democratic lean of 0.3 percentage points.

Mr. Obama’s national margin of victory has been inching up as the final outstanding votes are counted. If it eclipses his margin in Virginia, that state will remain — by a hair — a red one. But as things stand now, Virginia, for the first time since the 1948 election, has become — just barely — a blue state.