📅  最后修改于: 2023-12-03 14:46:52.134000             🧑  作者: Mango
本文介绍了可以在 R 中使用的一些常见图例类型和相关的 TypeScript 代码示例,帮助程序员更好地理解和使用这些图例。
点图例通常用于标识散点图中不同类型的数据点。
# 创建一个散点图
plot(mpg ~ wt, data = mtcars, pch = c(16, 17, 18, 19), col = c("blue", "red", "green", "purple"))
# 添加点图例
legend("topright", legend = c("A", "B", "C", "D"), pch = c(16, 17, 18, 19), col = c("blue", "red", "green", "purple"), title = "Legend Title")
颜色填充图例通常用于标识不同类别或组之间的颜色区分。
# 创建一个颜色填充的散点图
plot(mpg ~ wt, data = mtcars, pch = 19, col = factor(mtcars$gear))
# 添加颜色填充图例
legend("topright", legend = c("3 gears", "4 gears", "5 gears"), pch = 19, col = c("red", "blue", "green"), title = "Legend Title")
线型图例通常用于标识不同线条类型或线型与颜色的组合。
# 创建一个线型图
plot(1:10, type = "n") # 创建一个空白图
lines(1:10, col = "blue", lty = 1)
lines(2:11, col = "red", lty = 2)
lines(3:12, col = "green", lty = 3)
# 添加线型图例
legend("topleft", legend = c("Type 1", "Type 2", "Type 3"), col = c("blue", "red", "green"), lty = c(1, 2, 3), title = "Legend Title")
图形图例通常用于标识不同类型的图形。
# 创建一个图形图例
plot(1:10, type = "n") # 创建一个空白图
points(1:10, pch = 16, col = "blue")
points(2:11, pch = 17, col = "red")
points(3:12, pch = 18, col = "green")
# 添加图形图例
legend("bottomleft", legend = c("Type 1", "Type 2", "Type 3"), pch = c(16, 17, 18), col = c("blue", "red", "green"), title = "Legend Title")
可以设置图例的标题和位置。
# 创建一个散点图
plot(mpg ~ wt, data = mtcars, pch = 19, col = "blue")
# 添加点图例,设置标题和位置
legend("bottomleft", legend = "Legend Title", pch = 19, col = "blue", title = "Legend Title")
除了上述常见的图例类型和设置,R 还提供了更多的图例设置选项,包括设置文字大小、背景色、边框等。
# 创建一个示例图
plot(1:10, type = "n") # 创建一个空白图
points(1:10, pch = 19, col = "blue")
# 添加图例,设置更多选项
legend("topleft", legend = "Legend Title", pch = 19, col = "blue", title = "Legend Title", cex = 1.2, bg = "lightgray", border = "black")
以上是一些常见的图例类型和设置的 TypeScript 示例代码,希望对你在使用 R 进行绘图时有所帮助。更多具体的图例设置选项和用法,请参阅 R 的帮助文档或在线参考资料。