Bacteria-plasmid associations can be mutualistic or antagonistic depending on the strength of positive selection for plasmid-encoded genes, with contrasting outcomes for plasmid stability. In mutualistic environments, plasmids are swept to high frequency by positive selection, increasing the likelihood of compensatory evolution to ameliorate the plasmid cost, which promotes long-term stability. In antagonistic environments, plasmids are purged by negative selection, reducing the probability of compensatory evolution and driving their extinction. Here we show, using experimental evolution of and the mercury-resistance plasmid, pQBR103, that migration promotes plasmid stability in spatially heterogeneous selection environments. Specifically, migration from mutualistic environments, by increasing both the frequency of the plasmid and the supply of compensatory mutations, stabilized plasmids in antagonistic environments where, without migration, they approached extinction. These data suggest that spatially heterogeneous positive selection, which is common in natural environments, coupled with migration helps to explain the stability of plasmids and the ecologically important genes that they encode.