0

I have a problem with displaying the legend on the chart. Using aes_string does not appear to me. Could you help me to show some kind of legend on the chart?

The data I have is the following:

> head(rev_countries)
             US         China      India      Korea      Taiwan    Alemania     Francia      Espana
1  0.0016930023 -0.0203939098 0.01132170 -0.1148745 -0.04096485 -0.03902919 0.006571760 0.002196811
2  0.0016930023 -0.0203939098 0.01132170 -0.1148745 -0.04096485 -0.03902919 0.006571760 0.002196811
3  0.0016930023 -0.0203939098 0.01132170 -0.1148745 -0.04096485 -0.03902919 0.006571760 0.002196811
4  0.0075516046 -0.0005587373 0.02947631 -0.1113395 -0.04750715 -0.03585176 0.013940910 0.009494249
5 -0.0005482551 -0.0122860298 0.01092174 -0.1088372 -0.05022913 -0.04502686 0.003310547 0.002957849
6 -0.0005482551 -0.0122860298 0.01092174 -0.1088372 -0.05022913 -0.04502686 0.003310547 0.002957849
       Italia         UK        Japon      Brazil      Mexico       date    rev.1m.us rev.1m.China
1 0.001235253 0.01923392 -0.007952821 0.013958126 0.015917230 2018-12-31 -0.004314109 -0.005530346
2 0.001235253 0.01923392 -0.007952821 0.013958126 0.015917230 2019-01-01 -0.004314109 -0.005530346
3 0.001235253 0.01923392 -0.007952821 0.013958126 0.015917230 2019-01-02 -0.004314109 -0.005530346
4 0.008008179 0.02326827 -0.001905986 0.020438684 0.021886192 2019-01-03  0.001509360  0.014605786
5 0.014848945 0.01061286 -0.007897650 0.006886375 0.007849294 2019-01-04  0.001509360  0.014605786
6 0.014848945 0.01061286 -0.007897650 0.006886375 0.007849294 2019-01-07 -0.005461343 -0.001674341
  rev.1m.India rev.1m.Korea rev.1m.Taiwan rev.1m.Alemania rev.1m.Francia rev.1m.Espana rev.1m.Italia
1   0.01445868  -0.06991754   0.008796213    -0.009619475   -0.003276670 -0.0021365882  -0.009794952
2   0.01445868  -0.06991754   0.008796213    -0.009619475   -0.003276670 -0.0021365882  -0.009794952
3   0.01445868  -0.06991754   0.008796213    -0.009619475   -0.003276670 -0.0021365882  -0.009794952
4   0.03266960  -0.06620301   0.001914454    -0.006344804    0.004020380  0.0051292964  -0.003096640
5   0.03266960  -0.06620301   0.001914454    -0.006344804    0.004020380  0.0051292964  -0.003096640
6   0.01594724  -0.05313210  -0.013961667    -0.014044431   -0.001934658 -0.0002596428  -0.012555487
     rev.1m.UK rev.1m.Japon rev.1m.Brazil rev.1m.Mexico
1 -0.009122300 -0.004201523    0.02262443  -0.007001167
2 -0.009122300 -0.004201523    0.02262443  -0.007001167
3 -0.009122300 -0.004201523    0.02262443  -0.007001167
4 -0.005200192  0.001868177    0.02916038  -0.001166861
5 -0.005200192  0.001868177    0.02916038  -0.001166861
6 -0.009667177 -0.002055168    0.01891488  -0.008494208


The code I use is this:

bucle<-list()

for (i in 1:(length(rev_countries1m)-1)) {

 

  bucle[[i]]<-ggplot(rev_countries)+

    geom_line(aes_string(x="date",y= names(rev_countries)[i], fill=shQuote("b")), size=1.4, color="midnightblue")+

    geom_line(aes_string(x="date",y= names(rev_countries)[i+14], fill=shQuote("r")), linetype="twodash", size=1.4)+

    labs(x= "Date", y=" 1M, 3M Revisions", title =names_countries[i] , subtitle = "Next 12 Months EPS", color="Legend")+

    guides(color=TRUE)+

    scale_fill_manual(name="s", guide='legend', values = c("b"="black", "r"= "red"))+

    theme_economist()

}

 

bucle[[2]]

And the graph is this:

click to see the graph

I would like to add a legend by colors. Thanks in advance

r2evans
  • 141,215
  • 6
  • 77
  • 149
GFHR
  • 11
  • 1
  • 1
    These might help https://stackoverflow.com/a/52045613/786542 and https://stackoverflow.com/a/55524126/786542 – Tung Aug 21 '20 at 05:34
  • 1
    I think you want to use color= rather then fill= with lines. Otherwise I’m not sure what your trying to fill in – MrFlick Aug 21 '20 at 05:35

0 Answers0