There are a number of tricks and mindgames which can help you in a situation like this. The first thing I'd like to say is try to stay away from the edge, play center stage. If you do find yourself going off the edge here are a few things you can do.
1. Save your second jump, don't use it until you come near the edge. If your opponents are anything like my own, they look to edgehog you and they'll hop right on the edge thinking you've got no more jumps. At which point, you jump on the stage and Up B if necessary, for that added boost in height to get over the lip of the stage.
2. When your at higher percentages, you should be aware that Purple Pikmin are now your best friends. They create opening for you to not only attack, but to recover safely. As I said earlier, save your second jump. As you approach the ledge, don't panic and go balistic. Carefully wait until your in like and throw the Purple Pikmin at the person. They will either get off the ledge or take the hit. If they take the hit, the Pikmin can potentially kill them at higher percentages. If they get off the ledge using invincible frames to avoid the hit, get angled up with the ledge and use Up B to recover.
3. If your 2nd jump didn't quite get you over the ledge, and you would like an opponent off the ledge you can use neutral air. Neutral air has multiple hits and will knock the opponent off because it has a fairly long hitbox and stays out a while. At which point, Up B your way back on to the edge.
4. If your coming in on a 45% angle, you might want to f-air your opponent. If you smack them into the stage, the effect is similar to that of a spike and they'll usually die, either alone or with you. =P You can also give U-Air a shot, it has more upward reach, so if you know your not going to land a f-air, use this one. The only problem with up air is that the animation is a little long and after you land it, you may not have enough reach with the U-B to grab the ledge.
5. This one is a bit risky, but it plays a nice mind game for the opponent. If your at a high percentage along with your opponent, kill the both of you. Line yourself up, and launch a Pikmin Spear (U-B) straight into their back. Try and have Red or Purple at the end, since they have the most knockback. If done correctly, your opponent will be sent flying to their death, faster than you! That means if there is absolutely no way around it, and your on your last stock, you win!
6. If your off screen, you may want to try tossing all your Pikmin off the screen to die. This sounds a little stupid, but if you do that Olimar's 2nd jump gets a lot more height. It's also possible for your Up B to grab the ledge even when your a ways from it. When you do this, your opponent will usually get off the ledge, because they do not expect you to be coming back. The only problem is, what to do once your back on the ledge? Simple! Down Tilt, knocks your opponent into the air at higher percentages and can juggle at lower percentages. There is also running, so yeah.
These are all the ways I could think of to get back on the ledge. If you manage to be agressive about it like myself, your opponents will think twice before ledgeguarding you. I hope this helps. =]