President Andrew Johnson was weary of the secretary of war he inherited from his predecessor, Abraham Lincoln. Every time he turned around, it seemed the secretary was undermining the president's stature while confounding his policies.

So Johnson fired Edwin Stanton in February 1868. In Stanton's place, he named Lorenzo Thomas, an Army general of little distinction. Within a few days, the House of Representatives approved 11 articles of impeachment.

Facing the political fight of his life, Johnson clung to his office by a single vote in the Senate trial of his case. One of the key steps he took to save his job? Naming a respected Army general, John Schofield, as secretary of war in place of the widely disparaged Thomas. By that act, Johnson demonstrated his respect for the office of secretary of war and for his own duties under the Constitution.

No historical analogy is perfect, but Johnson's case from 150 years ago was the closest the nation has come to the forcible removal of a president from office. Johnson's maneuvering shows that it is possible for a president to survive the political fallout from a controversial firing, but it helps if the firing is followed by the appointment of a worthy successor. This resonates powerfully in the aftermath of this week's unexpected firing of FBI Director James B. Comey.

By February 1868, Johnson had been trying for months to drive Stanton from his Cabinet. The war secretary opposed Johnson's policy of leniency toward Southern states that had seceded from the Union. Working with the Republican Congress and with Gen. Ulysses S. Grant, Stanton thwarted Johnson by continuing to push for the deployment of federal troops to protect the lives and rights of freed slaves who faced violent repression throughout the former Confederacy.

In the words of one general, Johnson "disregarded the will of Congress, and the officers of the army disregarded his. The situation was approaching mutiny on one side, or treason on the other."

Firing Stanton was bound to be contentious. Congress had adopted the Tenure of Office Act specifically to limit Johnson's ability to replace officials such as the war secretary. For a president with a reputation of intemperate public statements and erratic decisions, the firing fostered doubts about his competence.

Yet other considerations supported Johnson's move. Presidents cannot govern with Cabinet officers who oppose their policies. And Stanton, often prickly even with friends and allies, was not a popular figure.

Replacing Stanton with a cipher such as Thomas, however, was a major blunder. Thomas' fecklessness was inescapable. When he tried to assume the duties of war secretary, Stanton drove him from the building by sheer force of will. For three months, the nation had two war secretaries: Stanton camped inside the headquarters while Thomas wandered the streets of Washington looking for something to do.

Shortly before the Senate's final impeachment vote, a key senator met privately with Johnson and delivered his message. Republican Sen. James Grimes of Iowa wanted to vote for acquittal, but first Johnson had to "quit his foolery with Lorenzo Thomas."

The president got the message. He replaced Thomas with Schofield, a West Pointer with a solid war record and a reputation for political adroitness. The president didn't care much for Schofield, but he finally realized he needed a better appointee than Thomas. Mollified by Johnson's gesture, Grimes and barely enough other Republicans voted to keep him in office.

In today's sped-up world, presidents no longer have three months to correct the appointment of an unworthy successor. When Trump designates a new FBI director, he will make a definitive statement about his commitment to the rule of law and the independence of the nation's judicial processes - subjects on which his previous actions and statements are far from reassuring.

He can learn from Johnson's experience, or he can risk his presidency. The decision is his.

Stewart is the author of "Impeached: The Trial of President Andrew Johnson and the Fight for Lincoln's Legacy."