Objectives: The pathogenesis of heterogeneity in gastric cancer (GC) is not clear and presents as a significant obstacle in providing effective drug treatment. We aimed to identify subtypes of GC and explore the underlying pathogenesis.Methods: We collected two microarray datasets from GEO (GSE84433 and GSE84426), performed an unsupervised cluster analysis based on gene expression patterns, and identified related immune and stromal cells. Then, we explored the possible molecular mechanisms of each subtype by functional enrichment analysis and identified related hub genes.Results: First, we identified three clusters of GC by unsupervised hierarchical clustering, with average silhouette width of 0.96, and also identified their related representative genes and immune cells. We validated our findings using dataset GSE84426. Subtypes associated with the highest mortality (subtype 2 in the training group and subtype C in the validation group) showed high expression of SPARC, COL3A1, and CCN. Both subtypes also showed high infiltration of fibroblasts, endothelial cells, hematopoietic stem cells, and a high stromal score. Furthermore, subtypes with the best prognosis (subtype 3 in the training group and subtype A in the validation group) showed high expression of FGL2, DLGAP1-AS5, and so on. Both subtypes also showed high infiltration of CD4+ T cells, CD8+ T cells, NK cells, pDC, macrophages, and CD4+ T effector memory cells.Conclusion: We found that GC can be classified into three subtypes based on gene expression patterns and cell composition. Findings of this study help us better understand the tumor microenvironment and immune milieu associated with heterogeneity in GC and provide practical information to guide personalized treatment.