Several Justices retired after Perez and before Afroyim:
- Harold Burton concurred with the Perez decision and was replaced by Potter Stewart (appointed by Dwight Eisenhower) who dissented in the Afroyim decision.
- Felix Frankfurter wrote the majority opinion in the Perez decision and was replaced by Arthur Goldberg and then Abe Fortas (appointed by Lyndon B. Johnson) who concurred in Afroyim
- Charles E. Whittaker dissented against the Perez decision and was replaced by Byron White (appointed by John F. Kennedy) who dissented against the Afroyim decision.
This was one new vote against the Perez position and one in favor of it. Net result would have kept the decision the same. All this is from the two Wikipedia articles (and the linked Justice articles for the replacements).
It was asserted in a comment that the Perez decision was actually 6-3 on the relevant point for Afroyim. I can neither confirm nor deny that, but if true, it would have had to have been Whittaker. Whittaker was the only one to write a solo dissent. The replacement by White would thus have changed that seat's position, dropping the 6-3 to 5-4.
One justice, William J. Brennan concurred with both decisions, changing his position. That changed the direction of the 5-4 decision. Apparently he simply changed his mind. In the Kennedy v. Mendoza-Martinez opinion, Brennan said:
The instant cases do not require me to resolve some felt doubts of the correctness of Perez, which I joined.
So by the time of that case, he was feeling doubts. By the time of Afroyim, those doubts turned to opposition.
There doesn't seem to be much in the way of politics involved. Two of the four Eisenhower appointees, the lone JFK appointee, and one of the two Franklin D. Roosevelt appointees dissented against the Afroyim decision. An FDR, two Eisenhowers, a Harry S Truman, and a Lyndon B. Johnson appointee voted for the majority. So both directions were bipartisan.
Justices are difficult to influence anyway. They have lifetime terms and none have been removed for cause. The most credible threat was FDR's court packing plan, but no one else has revived that since.
It's possible that at the time that Perez was argued, Brennan felt indebted to Eisenhower for the nomination and supported Eisenhower's position. This is a relatively close point, so Brennan may not have had strong feelings about it. Perez was in 1957-8, so Eisenhower was president. Afroyim was decided while Johnson was president, and Brennan didn't owe him anything. This is just speculation, as Brennan doesn't seem to have said what had been different.