Ggplot2 - I can't insert the chart legend

SlowLearning

Good evening everybody, The data are:

Data      X     Y
01/01/16 17073 229
01/02/16 16094 375
01/03/16 17380 880
01/04/16 19993 9978
01/05/16 26290 24782
01/06/16 32982 36437
01/07/16 38490 42547
01/08/16 36688 43928
01/09/16 22799 36734
01/10/16 15000 11816
01/11/16 10494 680
01/12/16 10944 434
01/01/17 17217 235
01/02/17 15501 466
01/03/17 19236 1608
01/04/17 22239 8490
01/05/17 30390 23374
01/06/17 35579 34568
01/07/17 39613 43283
01/08/17 44089 44741
01/09/17 25542 35611
01/10/17 16357 10131
01/11/17 11754 541
02/12/17 11583 362

I have a little problem with my chart.

I wrote this code:

ggplot() 
+ geom_line(data=DB_Reg, aes(x=DB_Reg$Data,
 y=DB_Reg$X), color='435',size=0.5, show.legend = TRUE)+
geom_line(data=DB_Reg, aes(x=DB_Reg$Data, y=DB_Reg$Y),color='534',size=0.5, show.legend = TRUE)+
ggtitle("XY")+
  xlab("Dates")+
ylab("Quantity")+
geom_point()

enter image description here

I'd like to put the legend about the two lines. I wrote show.legend=TRUE for the two lines, but doesn't work.

Jo Harris

When using ggplot it is very important to ensure the code is written in the correct order. For example, adding geom_point() at the end is going to overwrite the previous argument. Try removing it. Here is a script that should work.

  ggplot(data=DB_Reg)+
  geom_line(mapping=aes(y=X,x= Data,color="X"),size=1 ) +
  geom_line(mapping=aes(y=Y,x= Data,color="Y"),size=1) +
  scale_color_manual(values = c(
    'X' = 'darkblue',
    'Y' = 'red')) +
  labs(color = 'Y series')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I change the legend linetype in ggplot2?

How can I replace legend key in ggplot2?

How can I edit legend using ggplot2 in R?

How can I change the legend labels in R (ggplot2)

Can I group items in a legend in ggplot2?

How to hide chart legend in ggplot2?

Adding a bar chart to a ggplot2 legend

Creating a legend for a gantt chart with ggplot2

How can I completely remove legend.key from ggplot2 legend?

R - ggplot2 : insert subheadings in a legend

Can't add legend to ggplot2 figure - scale_color_manual() does nothing

How can I match the colors of lines and legend entries in ggplot2?

How can I manually place the NA entry in R ggplot2 discrete map legend?

How can I add a different legend for different quantiles in the same graph in ggplot2?

Can I place my ggplot2 legend right beside the horizontal axis label?

How can I edit the common legend title name using ggplot2 and ggpubr?

How can I add the legend for a line made with stat_summary in ggplot2?

How can I add a legend that counts points above or below a certain value in ggplot2? Volcano Plot

How can I combine two ggplot2 custom functions makig the legend separate

Why i can't plot a smoothing curve in ggplot2

How can I rearrange my legend and move it closer to this stacked bar chart in ggplot?

Create legend for line chart R GGPlot2

How to reduce the legend symbol thickness for a bar chart in ggplot2

Add values to pie chart legend with ggplot2 in R

Ordering Legend in Multi-Bar ggplot2 Chart

Insert data of two columns in a graph legend - ggplot2

How can I move the percentage labels outside of the pie chart in ggplot2?

How can I get geom_errorbar to "dodge" correctly on a bar chart in ggplot2?

Can't Get Telerik Legend to work with Bar Chart