Wild isolates of Caenorhabditis elegans differ in their tendency to aggregate on food [1, 2]. Most quantitative variation in this behavior is explained by a polymorphism at a single amino acid in the G protein-coupled receptor NPR-1: gregarious strains carry the 215F allele, and solitary strains carry the 215V allele . Although npr-1 regulates a behavioral syndrome with potential adaptive implications, the evolutionary causes and consequences of this natural polymorphism remain unclear. Here we show that npr-1 regulates two behaviors that can promote coexistence of the two alleles. First, gregarious and solitary worms differ in their responses to food such that they can partition a single, continuous patch of food. Second, gregarious worms disperse more readily from patch to patch than do solitary worms, which can cause partitioning of a fragmented resource. The dispersal propensity of both gregarious and solitary worms increases with density. npr-1-dependent dispersal is independent of aggregation and could be part of a food-searching strategy. The gregarious allele is favored in a fragmented relative to a continuous food environment in competition experiments. We conclude that the npr-1 polymorphism could be maintained by a trade-off between dispersal and competitive ability.