how to add a legend in ggplot in r

I would like to include a legend inside the top right of my plot, indicating the parameter values of the plots.

ggplot() + 
  geom_line(data=data1, aes(x=x, y=y1), color='green', linetype = "twodash", size=0.5) + 
  geom_line(data=data2, aes(x=x, y=y2), color='red', linetype="longdash", size=0.5) +
  geom_line(data=data3, aes(x=x, y=y3), color='blue') +
  theme_classic() +
  labs(x='input, x',
       y='output, f(x)')

Can someone please say how this is done. Thanks.

Upvotes: 0

Views: 100

Answers (1)

damo
damo

Reputation: 473

I think this is a very good source of "what to do with ggplot" http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#changing-the-position-of-the-legend

from there

# Position legend in graph, where x,y is 0,0 (bottom left) to 1,1 (top right)
bp + theme(legend.position=c(1, 1))

should/could do it?

Upvotes: 1

Related Questions