'The Andy Griffith Show' ran for 249 episodes/8 seasons from 1960 to 1968. From what I've read, Andy decided to leave the show to pursue a movie career and other projects. 'Mayberry R.F.D.' was a spin-off/continuation of 'The Andy Griffith Show' that still had many of the same characters. It ran for 78 episodes/3 seasons from 1968 to 1971 - Andy Griffith appeared in 6 episodes.
'Mayberry R.F.D.' was cancelled as part of CBS' "rural purge" where they wanted to make more politically correct shows targeted at a younger, more urban audience. Other victims of the purge were 'The Beverly Hillbillies', 'Petticoat Junction' and 'Green Acres'. A network cancelling a popular rural-based TV show, now that sounds familiar...