r - 使用 R 将图例添加到绘图中

标签 r plot legend

我使用 R 在一个图中创建了 4 条线。 代码在这里

# 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)

我想知道如何为图中的每个图(线)添加图例。

谢谢

最佳答案

legend( "topleft", c("cars", "trucks", "cars1", "trucks1"), 
text.col=c("blue", "red", "yellow", "green") )

使用 ?legend 命令查看选项,例如图形上的其他位置,除了“左上角”或文本大小之外,是否在图例周围有一个框等。

关于r - 使用 R 将图例添加到绘图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37401702/

相关文章:

r - 一个内核上的所有作业都因 R 多核而失败

r - data.table : how to improve performance 中的扩展窗口(累积计算)

python - 如何用 distplot 绘制两个图例

python - 在使用seaborn时的jointplot中,如何在图中设置另一个图例

r - 复制带有锁定变量的 R5 引用类

r - 根据行值中的条件创建计数器

python - 你如何在没有评估的情况下定义长符号表达式的实部/虚部?

R:使用 grid.echo 将 Plot 转换为网格失败,y 轴反向

javascript - ChartJs 强制散点图为正方形

r - 将关键图例添加到 R 中的多直方图