Much attention has been paid to President Trump's approval ratings throughout his term. A new survey asked respondents to put his performance into historical context — by comparing him with past presidents. 

About 1 in 5 adults surveyed said Trump has done the best or second-best job of any president in their lifetimes, according to the Pew Research Center. Pew surveyed 2,002 adults in June to determine the rankings.

The president who earns the most favorable ratings is the one Trump criticizes more than any other: his immediate predecessor, Barack Obama. More than 4 in 10 — 44 percent — said Obama is the best or second-best president in their lifetimes. Thirty-one percent named Obama as their favorite — an 11-point increase since 2011, late in his first term, when it was 20 percent.

Obama fares best with millennials, the largest generation in the country, with 46 percent of them ranking him as their favorite. Obama's higher popularity is not likely to be well received by Trump, who frequently points to perceived shortcomings of the previous administration when making decisions. But Trump is less than halfway through his term, so there is time for his relatively low popularity to grow. And the poll has some bright spots for him.

Trump fares best with baby boomers, people ages 54 to 72. More than 1 in 5 Americans in that age bracket — 21 percent — consider him the best or second-best president in their lifetimes. But the group's favorite president is one whom conservatives point to regularly as the standard for modern Republican presidencies: Ronald Reagan. More than 4 in 10 — 42 percent — consider Reagan among the best presidents in their lifetimes. 

However, despite Trump faring well with boomers, more Americans — 32 percent — in that age group point to Obama as their favorite president.

And even though Trump enjoys great popularity among Republicans — 88 percent of GOP voters approve of him, according to Gallup — he trailed Reagan among GOP voters picking their “best president” in the Pew poll. Nearly 60 percent of Republicans and Republican-leaning independents rank Reagan among the top two presidents in their lifetimes, while 40 percent name Trump.

Trump is still shaping his legacy — this week alone, he has taken major actions on the judiciary, foreign policy and trade that could help define it. But it is not clear whether history will judge those actions favorably or whether they will be enough to increase his popularity among future generations or even those in his own party.