-2

In R plot (not in ggplot2) how to put multiple legends with different titles?

plot(sin(1:100), type="l", col="red")
lines(cos(1:100), col="blue", lty=2)
legend("bottom", legend=c("Sin"), fill=c("red"), title="Sin Plot")
legend("bottom", legend=c("Cos"), fill=c("blue"), title="Cos Plot")

The second legend "Cos" covers the first one! How do I put second legend bellow fist one with Title?

I am not looking for multipal legends only But multiple legends with DIFFERENT TITLES. For example in this figure:enter image description here

http://i2.wp.com/www.milanor.net/blog/wp-content/uploads/2015/11/final-1.png?zoom=1.5&w=456

d.putto
  • 7,185
  • 11
  • 39
  • 45
  • @zx8754 No IT is not duplicate question. Please read the question fully before putting negative vote! I an NOT looking for multiple legends BUT legends with Multiple Titles. There is a difference between them. See the figure for explanation. – d.putto Jul 18 '16 at 09:31
  • From the example ggplot image, it is a multiple legend? – zx8754 Jul 18 '16 at 09:34
  • @zx8754 I know that. Again multiple legend is not a problem. I want legends with different titles. In the example ggplot image there are 2 titles: "CI horizontal line" and "Group" . That is what I want. – d.putto Jul 18 '16 at 09:37
  • @RicardoCruz I am using base. I wrote in 1st line "not on ggplot2". Why is it still confusing? – d.putto Jul 18 '16 at 09:39
  • Do you want legends exactly as it is in the ggplot, i.e.: `title, description, title, description`? Or `title, title, description, description`? And does it have to be outside or inside the plot? – zx8754 Jul 18 '16 at 09:44
  • @zx8754 Exactly as it is in the ggplot `title, description, title, description` – d.putto Jul 18 '16 at 09:46
  • Which means this post is a duplicate... you may close it, below answer proves it. – zx8754 Jul 18 '16 at 09:47
  • @zx8754 looks like you are still not reading the question and the duplicate question. You have to understand the difference. Here again in plan English: Question you refereed do not have any plot with "more then one Titles". I can't stress more on "Multiple Titles". Show me the plot with "multiple titles" in duplicate question, or tell why do you think still its duplicate? – d.putto Jul 18 '16 at 09:54
  • Last comment, from below answer you can see it is exactly the same line by line dupe of solution provided in the link. Set the par, then set xpd, only difference is they used legend function once, in this case we need to use it twice. – zx8754 Jul 18 '16 at 09:58
  • @zx8754 hmm "only difference is ...." and you think one difference is not enough!!! :) – d.putto Jul 18 '16 at 10:03

1 Answers1

1

Check if this is what you want:

par(xpd=TRUE, mar=c(4.5, 4.5, 1, 6))
plot(sin(1:100), type="l", col="red")
lines(cos(1:100), col="blue", lty=2)
legend(110, 0, legend=c("Sin"), fill=c("red"), title="Sin Plot")
legend(110, -0.5, legend=c("Cos"), fill=c("blue"), title="Cos Plot")

screenshot

You have to set par(xpd=TRUE) in order to disable clipping, and draw outside the plotting area. Then, adjust the margins accordingly, and set the legend manually where you want it.

Ricardo Magalhães Cruz
  • 3,504
  • 6
  • 33
  • 57