在R中创建具有匹配颜色和线形的图例

问题描述 投票:1回答:2

如何使用相应的线型和颜色创建合适的图例?另外,如何为此图创建标题?

在我的图例中,R对应于纯黑线,L是红色虚线,G是绿色虚线

以下是关于我如何创建图表和传奇的代码:

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

r plot graph legend
2个回答
2
投票

您可以为图例使用ltycol参数(您可以在此处找到不同的线型值:http://www.cookbook-r.com/Graphs/Shapes_and_line_types/)。对于标题,您可以使用main参数。

## Plot
matplot(x_values, cbind(r_y_values, l_y_values, g_y_values), type = 'l', xlab='N', ylab='Time(ms)', main = "my title")
## Line type parameters
par_lty <- c(1,2,3)
## Line colours parameters
par_col <- c("black", "red", "green")
## Legend
legend("topleft", legend = c("R", "L", "G"), inset = 0.01, lty = par_lty, col = par_col)

5
投票

只需指定一个lty值。

legend("topleft", legend=c("R", "L", "G"), col=c(1:3), lty=1:3, inset=0.01)
© www.soinside.com 2019 - 2024. All rights reserved.