-2

I am trying to make ggplot with more than 10 plots in one graph. However, I realized that my expertise does not work as I noticed the order of legend is not exactly the same as they appear in the figure. Is there any other way to add legend without destroying the structure? Many many thanks. Below is the code for your reference:

Figure1RCP45 <- ggplot(dataSet1, aes(x = Year)) +
  geom_line(aes(y = avgHist, colour = 'HISTORICAL')) +
  geom_line(aes(y = ACCESS1, colour = 'ACCESS1')) +
  geom_line(aes(y = CANESM21, colour = 'CANESM21')) +
  geom_line(aes(y = CAESM1BGC, colour = 'CAESM1BGC')) +
  geom_line(aes(y = CCSM4, colour = 'CCSM4')) +
  geom_line(aes(y = CMCC_CMS, colour = 'CMCC_CMS')) +
  geom_line(aes(y = CNRCCM5, colour = 'CNRCCM5')) +
  geom_line(aes(y = GFDL_CM3, colour = 'GFDL_CM3')) +
  geom_line(aes(y = HADGECC, colour = 'HADGECC')) +
  geom_line(aes(y = HADGEES, colour = 'HADGEES')) +
  geom_line(aes(y = MICRO5, colour = 'MICRO5')) +
  geom_line(aes(y = avgProj, colour = 'PROJECTED AVERAGE'), size = 1) +
  labs(
    title = 'First Cycle',
    x = 'Year',
    y = 'Days to mature',
    color  = 'Legend\n'
  ) +
  scale_color_manual(
    name = 'Legend',
    values = c(
      'HISTORICAL'        = 'black',
      'ACCESS1'           = 'cyan',
      'CANESM21'          = 'blue',
      'CAESM1BGC'         = 'orange',
      'CCSM4'             = 'yellow',
      'CMCC_CMS'          = 'green',
      'CNRCCM5'           = 'red',
      'GFDL_CM3'          = 'brown',
      'HADGECC'           = 'gray',
      'HADGEES'           = 'tomato',
      'MICRO5'            = 'salmon',
      'PROJECTED AVERAGE' = 'magenta'
    )
  )

Figure 2 review

1 Answers1

1

1) That's a really bad way to plot many lines on one graph (difficult to write, maintain, & read), you want to reshape your data. Look here Add legend to ggplot2 line plot

2) We can't give specific answers without a reproducible example How to make a great R reproducible example?

3) String values in scales (like you have with color) show in alphabetical order. If you want them in a specific order convert them to a factor and set the levels. Or you can set the breaks. How to reorder the items in a legend?

Jack Brookes
  • 3,720
  • 2
  • 11
  • 22
  • Thanks for prompt response. However, the steps in the previous blogs are not well enough to understand. Even though I tried some of them it is not what I want. Is there any way to make them perfect? Or there are some other package rather than ggplot... – Mahesh Maskey Jul 15 '18 at 22:26
  • Address point (2) and someone can provide a solution – Jack Brookes Jul 15 '18 at 22:28
  • I would be happy to get some assistance. If possible you can reach me at mmaskey@ucdavis.edu. – Mahesh Maskey Jul 16 '18 at 02:19
  • Finally it worked. However, it seems to be difficult to assign different line styles or thickness. Do you have any ideas?? – Mahesh Maskey Jul 20 '18 at 17:56
  • "different line styles or thickness" do you mean different for each value? In that case you can map size to your column that contains your grouping factor (`aes(size = ...) `). Then adjust the scale (`scale_size_manual(values = list(... = ...))`). If you just mean for all the lines go for `geom_line(size = ...)`. For line type, as above but replace `size` with `linetype` – Jack Brookes Jul 20 '18 at 20:38