在R语言中,我们可以使用col
参数来指定图例中线条的颜色。图例是数据可视化中常用的组成部分,它用于标识不同线条或数据系列,使得读者能够更好地理解图表。
下面我将为你展示如何使用col
参数来自定义图例中线条的颜色。让我们从创建一个简单的折线图开始,并对图例中的线条颜色进行设定。
首先,我们需要准备一些示例数据。以下是一个月度销售额的数据集:
# 创建示例数据
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
sales1 <- c(100, 150, 120, 200, 180, 250)
sales2 <- c(120, 130, 140, 160, 150, 170)
sales3 <- c(80, 90, 110, 100, 120, 130)
# 创建折线图
plot(x = months, y = sales1, type = "l", col = "red", ylim = c(0, 300), xlab = "Month", ylab = "Sales", main = "Monthly Sales")
lines(x = months, y = sales2, col = "blue")
lines(x = months, y = sales3, col = "green")
# 添加图例
legend("topleft", legend = c("Sales 1", "Sales 2", "Sales 3"), col =