A funny thing happened to Walmart (WMT) during the past five years: The retailer -- known for its Arkansas roots and its red-state appeal -- has made inroads with left-leaning shoppers.

A majority of shoppers who identify as Democrats now say they would consider shopping at Walmart, a shift from just five years ago when slightly more than four out of 10 said they would buy from the store, according to new data from YouGov BrandIndex, a service that tracks brand perception. Democrats also say they now view Walmart in a positive light, while five years ago their perception was negative.

The shift is notable for its large swing in just five years but also because it came without eroding support among Republican-leaning shoppers, said Ted Marzilli, chief executive officer of data products at YouGov. The reason, he said, may be due to a few changes taking place at Walmart, including investing in technology and new services that are wooing younger shoppers, who tend to be more liberal.

Get Breaking News Delivered to Your Inbox

Walmart has also increased its workers' pay and spoken out against anti-gay laws, leading the LGBT magazine The Advocate to remark on Walmart's "transformation into an LGBT ally."

"It is unusual to see a brand improve with one political party while maintain its ratings with another political party," Marzilli said. "It's a hard feat to pull off."

Some of Walmart's recent moves show how it can appeal to a wide range of groups. In late February, following the mass shooting in Parkland, Florida, the retailer raised the age to 21 for consumers who want to buy guns and ammunition. And just this week, following a push from family-friendly advocates, Walmart removed Cosmopolitan magazine from its checkout lanes.

In 2013, about 44 percent of Democratic shoppers said they would consider buying at Walmart, compared with 66 percent of Republicans. Skip ahead five years, and almost 52 percent of Democrats said they would shop at Walmart, while the Republican share was little changed.

Typically, brands aim to appeal to a broad demographic, under the theory that the more shoppers they reach, the better. Yet some brands end up aligned with one political philosophy, either accidentally or through a conscious decision to appeal to a smaller demographic.

Hobby Lobby, for instance, tends to attract conservative-leaning shoppers because of the company's stance against providing emergency birth control to its employees.

Starbucks (SBUX), however, has become associated with liberal shoppers, partly because of the views of its former CEO Howard Schultz. He vowed to hire 10,000 refugees after President Donald Trump introduced a ban on immigrants from several Muslim-majority nations.

Walmart, which didn't respond to a request for comment, has historically drawn criticism from progressive groups, which have criticized its low wages and resistance to labor unions, among other issues.

"It's always had a higher perception with consumers who associate with the Republican party and those who lean conservative," Marzilli said. "When it did expand, it expanded into more rural counties, and those areas tend to be more conservative."

The shift is more than academic for Walmart, Marzilli said. The change comes at a tough time for retailers, which are facing challenges from online stores such as Amazon (AMZN). In the last year, several big retailers -- like Toys "R" Us -- have filed for bankruptcy, partly from the impact of competition with Amazon and others.

Last year, Walmart's sales from its U.S. locations rose 2.8 percent. By comparison, revenue for discount department stores declined 1.5 percent in 2017, according to Commerce Department data.

"It's had an impact on Walmart's bottom line," Marzilli said.