1

I would like to label my graph so I don't need to use a legend but I don't know how to do it.

For example, using this data I get these 3 lines

x = c(1:10)
y = x^2
z = x^3
w = 2*x + 7

plot(x,y,type="l", col="red")
lines(z, type="l", col="blue")
lines(w, type="l", col="green")

https://gyazo.com/a674a148c57e38160a502f3f51a41046

And I want to label each graph, y, z and, w respectively. I want it to look like this

https://gyazo.com/93a9e055a02f42fb61c3e1e438485dee

Where each graph has a label by it, so a legend is not necessary

I looked at this thread How can i label points in this scatterplot?

But this is for a scatterplot and I wasnt sure how to do it for a continuous plot.

Gragbow
  • 177
  • 1
  • 9

2 Answers2

1

You can set xlab and ylab parameters of the plot function to get the labels on x and y axis respectively:

plot(x,y,type="l", col="red", xlab="time", ylab="concentration")

And then use text function to put labels on individual lines:

lines(z, type="l", col="blue")
lines(w, type="l", col="green")
text(10, 95, "R", cex = .8)
text(4, 100, "B", cex = .8)
text(10, 20, "G", cex = .8)

The plot now looks like:

Plot with text labels

See text function here https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/text.html

0

You can also use locator() within text and point and click on the desired coordinates of the labels such as:

plot(x,y,type="l", col="red")
lines(z, type="l", col="blue")
lines(w, type="l", col="green")
text(locator(), labels = c("y", "z", "w"), col=c("red","blue","green"))

enter image description here

DJack
  • 4,850
  • 3
  • 21
  • 45