I heard a great idea from a shiur by Rabbi Moshe Weinberger. He asks why we it is that we call Yosef "Yosef Hatzadik." When we compare Yosef with Yaakov Avinu, we see that Yaakov was completely perfect in the area of kedushas habris. Yosef, according to some opinions, actually had a fall according to his level (although that's hard for us to understand). According to this opinion, when the Torah says that Yosef came to the house of Potiphar to do his work, he actually was coming to be with eishes Potiphar. He had decided he couldn't take her attacks anymore, and he came to be with her when she was alone. Rabbi Weinberger explains that on some level there was a pgam here. So, wouldn't it make sense to Yaakov "Yaakov Hatzadik" instead of Yosef since he was the one who was perfect?
One way to answer this question is that Yosef became Yosef Hatzadik through the way that he responded to his slip. When Yosef tried to flee from eishes Potiphar she grabbed him by his clothes. We see the root of the word for garment (בגד) shares a root with the word for shame. We can read it as she tried to grab Yosef by his shame. "You already came here to see me anyway, clearly you're a low life, you should just go though with what you came here to do." Yosef doesn't let her grip him by his shame. He leaves behind his בגד and flees. He doesn't let eishes Potiphar convince him that he is a low life. Yes he made a mistake (on his level), but he is still Yosef. In this way he becomes Yosef Hatzadik. We all have an eishes Potiphar in our life. We're all incited to just give up our mission as a Jew. The key is in those moments is to say I'm still a Jew and to get out of there, not to be caught by your בגד. Yes I slipped up, but I still have my mission ahead of me and I'm committed to it. That's the nature of Yosef Hatzadik, and that's why we call him by that title. You can do this! שֶׁבַע יִפּוֹל צַדִּיק וָקָם