-2

I'm using visual studio with R version 3.5.1 where I tried to plot legend to the graph.

 f1 = function(x) {
     return(x+1)}

 x1 = seq(0, 1, by = 0.01)

 data1 = data.frame(x1 = x1, f1 = f1(x1), F1 = cumtrapz(x1, f1(x1)) )

However, when I tried to plot it, it never give me a legend! For example, I used the same code in this (Missing legend with ggplot2 and geom_line )

 ggplot(data = data1, aes(x1)) +
     geom_line(aes(y = f1), color = "1") +
     geom_line(aes(y = F1), color = "2") +
     scale_color_manual(values = c("red", "blue")) 

I also looked into (How to add legend to ggplot manually? - R ) and many other websites in stackoverflo, and I have tried every single function in https://www.rstudio.com/wp-content/uploads/2016/11/ggplot2-cheatsheet-2.1.pdf

i.e.

     theme(legend.position = "bottom")
     scale_fill_discrete(...)
     group
     guides()
     show.legend=TRUE

I even tried to use the original plot() and legend() function. Neither worked.

I thought there might be something wrong with the dataframe, but I split them(x2,f1,F1) apart, it still didn't work.

I thought there might be something wrong with IDE, but the code given by kohske acturally plotted legend!

 d<-data.frame(x=1:5, y1=1:5, y2=2:6)
 ggplot(d, aes(x)) + 
   geom_line(aes(y=y1, colour="1")) + 
   geom_line(aes(y=y2, colour="2")) +
   scale_colour_manual(values=c("red", "blue"))

What's wrong with the code?

J C
  • 181
  • 7
  • 1
    You're using `ggplot2` the wrong way: transform it from wide to long format and use variable within `aes`. Something like this: `ggplot(reshape2::melt(data, "x1"), aes(x1, value, color = V1)) + geom_line()`. [Related question1](https://stackoverflow.com/questions/3427457/ggplot-and-r-two-variables-over-time); [Related question2](https://stackoverflow.com/questions/32486446/specific-variable-color-in-ggplot) – pogibas Sep 30 '18 at 15:33
  • @PoGibas What do you mean? I tried melt(data1, id = "x2") whcih R said it could not find function melt, and I have just typped in ggplot(reshape2::melt(data1, "x1"), aes(x1,value, color = c("blue", "red"))) it's also error – J C Sep 30 '18 at 15:37
  • `melt` comes from package `reshape2` – pogibas Sep 30 '18 at 15:37
  • @PoGibas Thanks but I installed it. install.packages("reshape2") (and it checked as success ) then library(reshape2) returned an error message. What's is reshape doing anyway, I mean why using it? why kohske's code worked but not mind? I mean, I Iliterally replaced the d in his code, then there is no legend. – J C Sep 30 '18 at 15:45
  • Please look at attached question, there they have similar problem as you. Sorry, I don't know why `reshape2` doesn't work, but it's not related to original question. – pogibas Sep 30 '18 at 16:00
  • @PoGibas I see what it's doing, so melt put associate a new column of catalog variable to the original data, and ggplot picks up that data right? Could you explain to me in a few words that what is the "variable" doing? (how it knows to pick up the numerical value?) and if I can just pick up $f1's$ legend? – J C Sep 30 '18 at 16:14

1 Answers1

0

As far as I know, you only have X and Y variables in your aesthetics. Therefore there is no need for a legend. You have xlab and ylab to describe your two lines. If you want to have legends, you should put the grouping in the aesthetics, which might require recoding your dataset

d<- data.frame(x=c(1:5, 1:5), y=c(1:5, 2:6), colorGroup = c(rep("redGroup", 5), 
rep("blueGroup", 5)))
ggplot(d, aes(x, y, color = colorGroup )) + geom_line()

This should give you two lines and a legend

  • I have $x$, $f1$, and $F1$ three variables, and I plotted $f1$ and $F1$ with respec to $x1$ – J C Sep 30 '18 at 15:39
  • I worked from reproducible example you put in the bottom of your question. Can't do your programming for you :) as @PoGibas wrote, melt it with reshape for the same results. – Lukáš Hejtmánek Sep 30 '18 at 15:40