Pattern formation in a sandpile is investigated by pouring a ternary mixture of grains into a vertical narrow cell. Size segregation in avalanches causes the formation of patterns. Four kinds of patterns emerge: stratification, segregation, upper stratification-lower segregation, and upper segregation-lower stratification. A phase diagram is constructed in a parameter space of θ(11)/θ(33) and θ(22)/θ(33), where θ(11),θ(22), and θ(33) are the repose angles of small, intermediate, and large grains, respectively. To qualitatively understand pattern formation, a phenomenological model based on a roll-or-stay rule is proposed. A similar pattern formation is found in a numerical simulation of the phenomenological model. These results suggest that the ratios of the repose angles of three kinds of grains are important for pattern formation in a sandpile.