0

I have regions as categorical variable and multiple countries under each region. If I plot multiple line chart for each region using facet_wrap I can see a plot per region but the legend is quite obscure as its not organised as per Regions. How can I break the legends for each plot in facet_wrap showing only the countries within that group.

Region wise migration pattern

  1. I tried to manually colour the countries to make more sense (as by default same colours are repeated but didn't help)

Below is the code I tried for problem 2 but it didn't help.

ggplot(data = temp_tidy, aes(x= year,y= population)) + 
   geom_point()+geom_line(aes(group=`Country of birth`)) facet_grid(Major_Region_New~. )+
   scale_color_manual(values = c("red","blue","green","brown","yellow","pink","seagreen1",
                                 "gold1","sienna2","tomato","plum4","purple2"))

Actual plot should be 4 legends with 3 countries under each.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
analyst045
  • 628
  • 7
  • 21
  • Please make your question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon May 04 '19 at 11:38
  • 3
    Don't think you can make separate legends per facet, you'd probably be better off using `gridExtra::grid.arrange` or `cowplot::plot_grid` – erocoar May 04 '19 at 11:40
  • I have two suggestions. 1. Add ```aes(colour=`Country of birth`)```, put inside one of your two existing `aes()` calls, either one will work. 2. Make your values a named vector to control which colour goes with which country: `values = c("Greece"="blue", "Malaysia"="sienna2", "Sri Lanka"="gold1")` etc.. – bdemarest May 05 '19 at 02:21

0 Answers0