NEW YORK — His strokes awry, his emotions laid bare for all to see, Roger Federer figured out a way to stay in the U.S. Open.

Federer found himself locked in a five-set struggle against the sort of player the world is accustomed to seeing him dismiss with ease, and it was only down the stretch that the four-time defending champion at Flushing Meadows looked the part.

Avoiding as big an upset as tennis has seen in a while, Federer came back to beat 23rd-seeded Igor Andreev of Russia 6-7 (5), 7-6 (5), 6-3, 3-6, 6-3 Tuesday night to reach the quarterfinals at the year”s last Grand Slam tournament.

When Federer finished the match with a forehand winner, he shook his fists violently and yelled, then a flashed a grin toward his girlfriend and others in the player guest box.

Hard to recall the last time this guy was so pleased by a mere fourth-round victory, one that moved him into a match against qualifier Gilles Muller. Federer is, after all, a man who owns 12 Grand Slam titles, two shy of Pete Sampras” career mark. A man who has won 31 consecutive matches at the U.S. Open. A man who is trying to extend his record streak of 17 straight appearances in major semifinals.

And yet, because of how tough this was, Federer couldn”t stop smiling at the end of the 3½-hour test.

“I was just trying to stay in the match,” he told the supportive crowd at Arthur Ashe Stadium.

Novak Djokovic felt the same way about his own five-setter in the fourth round, on the same court earlier Tuesday. When Djokovic”s 4-6, 6-2, 6-3, 5-7, 6-3 victory over No. 15 Tommy Robredo of Spain was over, the 2007 runner-up to Federer at the U.S. Open and the reigning Australian Open champion looked up in the stands and saw his mom pounding her chest repeatedly.

Djokovic responded in kind, bumping a closed fist over his heart four times, then using his right index finger to point there, point to each knee and point to his temple — looking up at his guest box all the while.

“Just trying to show them, you know, how much effort I put into this match,” Djokovic said.

He needed every ounce of heart, smarts and energy he could muster, and acknowledged that his quarterfinal foe — 2003 U.S. Open champion Andy Roddick or No. 11 Fernando Gonzalez — would be fitter.

Djokovic was bothered at various points by his stomach, his hip, his right ankle and breathing problems.

“Somehow,” the No. 3 Djokovic said, “I managed.”

This has been a difficult year for the once-unbeatable Federer, whose 12 losses already are more than he absorbed in any entire season from 2004-07. The standards he has set are so high that any misstep is fodder for questions about the state of his game — and his career.

Plenty of top players would love to endure the sort of “down year” Federer has had. Don”t forget, he did reach the semifinals at the Australian Open and the finals at the other two major tournaments.

Still, it”s one thing to lose to Rafael Nadal, the man who walloped Federer in the French Open final and edged him in a five-set thrill ride of a Wimbledon final. Nadal does own five Grand Slam titles and has overtaken Federer in the rankings after the Swiss star”s record 237-week stay at No. 1.

It would have been quite another to lose to Andreev, someone who showed up at this U.S. Open with a career mark of 2-3 at the place, someone who has only once made it as far as the quarterfinals at any major tournament, someone who entered Tuesday on a seven-match losing streak against players ranked in the top five.

That”s not to say Andreev is a complete nobody. He knocked off Andy Roddick at the French Open last year. He is ranked in the top 25. But he is also now 1-7 in five-set matches, compared to Federer”s 12-11 mark, and experience certainly was a factor at key stages.

In the second set, for example, Andreev accumulated seven break points — and Federer saved them all. Any one of those could have swung the match for good. And in the crucible of the fifth set, Andreev compiled four more break points, all with Federer serving at 4-2 — and, again, Federer handled the situation better, erasing every one.

“Very important moment,” Andreev acknowledged.

Federer played poorly at times, and Andreev seemed as though he simply could not miss a shot for stretches, scurrying along the baseline to whip his go-for-it forehand and find a line. In the second set, for example, Andreev had 18 winners and only three unforced errors, a stunning ratio, particularly against Federer.

Indeed, Andreev managed to make the once-invincible Federer seem human — not just during points, but between them.

Normally so calm, so collected, Federer often threw his head back in disappointment or screamed with delight. He pulled a ball out his pocket and chucked it. He cracked another ball into the net after one lost opportunity.

But he was at his best when he absolutely needed to be, and was brilliant up at the net, winning 58 of 84 points when he pressed forward.

“I was struggling to get into the groove from the baseline,” Federer said.

His serve was broken in the match”s opening game, and then he blew a lead in the first tiebreaker. That set closed with a 13-stroke exchange that Federer ended by missing a forehand wide — already his 19th unforced error of the match, nearly twice as many as Andreev, so far.

There was more of the same, but in the end, Federer could rely on a muscle memory from big matches on big stages that Andreev could not.