  Low-energy excitations in graphene exhibit relativistic properties due to the
linear dispersion relation close to the Dirac points in the first Brillouin
zone. Two of the Dirac points located at opposite corners of the first
Brillouin zone can be chosen as inequivalent, representing a new valley degree
of freedom, in addition to the charge and spin of an electron. Using the valley
degree of freedom to encode information has attracted significant interest,
both theoretically and experimentally, and gave rise to the field of
valleytronics. We study a graphene $p$-$n$ junction in a uniform out-of-plane
magnetic field as a platform to generate and controllably manipulate the valley
polarization of electrons. We show that by tuning the external potential giving
rise to the $p$-$n$ junction we can switch the current from one valley
polarization to the other. We also consider the effect of different types of
edge terminations and present a setup, where we can partition an incoming
valley-unpolarized current into two branches of valley-polarized currents. The
branching ratio can be chosen by changing the location of the $p$-$n$ junction
using a gate.
