Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a legend in R with matching color and line shape

Tags:

graph

plot

r

legend

How can I create a proper legend with the corresponding line type and color? Also, how can I create a Title for this graph?

In my legend, R corresponds to the solid black line, L is the dashed red line, and G is the green dotted line

Here is the code on how I created my graph and my legend:

matplot(x_values, cbind(r_y_values, l_y_values, g_y_values), type = 'l', xlab='N', ylab='Time(ms)')
legend("topleft", legend=c("R", "L", "G"), col=c(1:3), inset=0.01)

enter image description here

like image 202
Liondancer Avatar asked Jan 23 '26 10:01

Liondancer


1 Answers

Just specify an lty value.

legend("topleft", legend=c("R", "L", "G"), col=c(1:3), lty=1:3, inset=0.01)
like image 136
G5W Avatar answered Jan 26 '26 00:01

G5W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!