使用R将图例添加到绘图中 [英] Add legend to a plot with R
本文介绍了使用R将图例添加到绘图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用R在一个绘图中创建4条线. 这里的代码
I create 4 line in one plot using R . Here the code
# Define 2 vectors
cars <- c(123.07, 110.51, 96.14, 98.71, 101.3)
trucks <- c(110.31, 89.91, 89.81, 89.31, 93.4, 95.81)
cars1 <- c(123.227, 110.221, 93.14, 98.22, 122.3)
trucks1 <- c(120.31, 89.91, 89.81, 89.31, 93.4, 95.81)
# Graph cars using a y axis that ranges from 0 to 12
plot(cars, type="o", col="blue", ylim=c(80,130))
# Graph trucks with red dashed line and square points
lines(trucks, type="o", pch=22, lty=2, col="red")
lines(cars1, type="o", col="yellow", ylim=c(80,130))
# Graph trucks with red dashed line and square points
lines(trucks1, type="o", pch=22, lty=2, col="green")
# Create a title with a red, bold/italic font
title(main="Autos", col.main="red", font.main=4)
我想知道如何为图中的每个图(线)添加图例.
I would like to know how can I add the legend for each plot (line) in the plot.
谢谢
推荐答案
legend( "topleft", c("cars", "trucks", "cars1", "trucks1"),
text.col=c("blue", "red", "yellow", "green") )
使用?legend命令查看选项,例如上左"或文字大小以外的图形上的其他位置,是否在图例周围有一个框,等等.
Use the ?legend command to see options, such other locations on the graph, besides "topleft" or text size, whether to have a box around the legend, etc.
这篇关于使用R将图例添加到绘图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文