Molecular switches are building blocks of potential interest to store binary information, especially when they can be organized in periodic lattices. Among the variety of possible systems, switches based on hydrogen transfer are of special importance because they allow the switching operation to occur without severe conformational change that may interfere with neighboring molecular units. We have studied the excitation process of hydrogen transfer inside porphyrin molecules assembled on a graphene surface, using a low-temperature scanning tunneling microscope. We show that this hydrogen transfer is induced by an electronic resonant tunneling process through the molecular orbitals. Using nitrogen doping of graphene, we tune the rate of hydrogen transfer by shifting the molecular orbital energies owing to the charge transfer at nitrogen dopant sites in the graphene lattice. The control of the switching process allows the storage of information inside a molecular lattice, which is demonstrated by writing an artificial pattern inside a molecular island.