Bacterial populations diversify during infection into distinct subpopulations that coexist within the human body. Yet, it is unknown to what extent subpopulations adapt to location-specific selective pressures as they migrate and evolve across space. Here we identify bacterial genes under local and global selection by testing for spatial co-occurrence of adaptive mutations. We sequence 552 genomes of the pathogen Stenotrophomonas maltophilia across 23 sites of the lungs from a patient with cystic fibrosis. We show that although genetically close isolates colocalize in space, distant lineages with distinct phenotypes separated by adaptive mutations spread throughout the lung, suggesting global selective pressures. Yet, for one gene (a distant homologue of the merC gene implicated in metal resistance), mutations arising independently in two lineages colocalize in space, providing evidence for location-specific selection. Our work presents a general framework for understanding how selection acts upon a pathogen that colonizes and evolves across the complex environment of the human body.