📜  Base R 图图例中不同颜色的点和线

📅  最后修改于: 2022-05-13 01:54:40.384000             🧑  作者: Mango

Base R 图图例中不同颜色的点和线

在本文中,我们将看到如何在基本 R 编程语言中绘制不同颜色的点和线。

创建用于演示的数据集:

R
A <- matrix( c( c(0,1,2,3,5), c(1,3,4,2,4)), ncol=2 )
B <- matrix( c( c(3,1,2,3,4), c(4,2,1,1,2)), ncol=2 )
C <- matrix( c( c(1,1,2,4,5), c(4,4,0,1,2)), ncol=2 )
data <- data.frame(A,B)
data


R
# Plotting the lines and points
# here type b means we are plotting
# both points and line
plot( 0, type = "b", xlim = c(0,5),
     ylim = c(0,5) )
 
# defining the line column and colour
lines( A, col = "red" )
 
# defining the point column and colour
# here pch means the type of symbol
# we can choose from 1 to 25
points( A, col = "yellow", pch = 16 )
lines( C, col = "green" )
points( C, col = "pink", pch = 15 )
lines( B, col = "blue" )
points( B, col = "violet", pch = 17 )


R
A <- matrix( c( c(0,1,2,3,5), c(1,3,4,2,4)), ncol=2 )
B <- matrix( c( c(3,1,2,3,4), c(4,2,1,1,2)), ncol=2 )
C <- matrix( c( c(1,1,2,4,5), c(4,4,0,1,2)), ncol=2 )
data <- data.frame(A,B)
 
plot( 0, type = "b", xlim = c(0,5),
     ylim = c(0,5) )
lines( A, col = "red" )
points( A, col = "yellow",
       pch = 16 )
lines( C, col = "green" )
points( C, col = "pink", pch = 15 )
lines( B, col = "blue" )
points( B, col = "violet", pch = 17 )
 
legend( x = "topright",
        legend = c("Red line, Yellow points","Blue line,
                  iolet points","Green line,Pink points"),
        col = c("red","blue","green"), lwd=1, lty=c(1,1,1),
        pch=c(NA,NA), cex = 0.5 )


R
# data
A <- matrix( c( c(0,1,2,3,5), c(1,3,4,2,4)), ncol=2 )
B <- matrix( c( c(3,1,2,3,4), c(4,2,1,1,2)), ncol=2 )
C <- matrix( c( c(1,1,2,4,5), c(4,4,0,1,2)), ncol=2 )
data <- data.frame(A,B)
 
# plot
plot( 0, type = "b", xlim=c(0,5), ylim=c(0,5) )
lines( A, col = "red" )
points( A, col = "yellow", pch=16 )
lines( C, col = "green" )
points( C, col = "pink", pch=15 )
lines( B, col = "blue" )
points( B, col = "violet", pch=17 )
 
# legend
legend( x="topright",
        legend=c("Red line, Yellow points","Blue line,
                iolet points","Green line,Pink points"),
        col=c("red","blue","green"), lwd=1, lty=c(1,1,1),
        pch=c(NA,NA),cex=0.5 )
 
legend( x="topright",
        legend=c("Red line, Yellow points","Blue line,
                violet points","Green line,Pink points"),
        col=c("yellow","purple","pink"), lwd=1, lty=c(0,0,0),
        pch=c(16,15,17),cex=0.5 )



输出:



让我们看看上面的代码,其中 'A','B','C' 是矩阵的名称,在 c() 中输入向量由值组成。它还包含行数,即,'ncol' 为二。

用不同颜色在图形上绘制线和点

创建矩阵后,现在我们将绘制线和点。

电阻

# Plotting the lines and points
# here type b means we are plotting
# both points and line
plot( 0, type = "b", xlim = c(0,5),
     ylim = c(0,5) )
 
# defining the line column and colour
lines( A, col = "red" )
 
# defining the point column and colour
# here pch means the type of symbol
# we can choose from 1 to 25
points( A, col = "yellow", pch = 16 )
lines( C, col = "green" )
points( C, col = "pink", pch = 15 )
lines( B, col = "blue" )
points( B, col = "violet", pch = 17 )

输出:



在上图中,我们绘制了线条和点。没有指定哪一行代表什么,为了做到这一点,我们将使用图例函数为它们提供相同的标签。

添加图例

向绘图添加图例时,有一种主要方法可以修改图例位置。您可以将参数 x 设置为“top”、“topleft”、“topright”、“bottom”、“bottomleft”、“bottomright”、“left”、“right”或“center”,在这种情况下,您不需要必须设置参数 y。

在下面的代码中,在绘制图形后,我们现在添加一个 pch=c(NA, NA) 的图例,这意味着图例部分没有显示“pch”,表示点符号,我们可以根据需要从 1 到 25用户的选择。

电阻

A <- matrix( c( c(0,1,2,3,5), c(1,3,4,2,4)), ncol=2 )
B <- matrix( c( c(3,1,2,3,4), c(4,2,1,1,2)), ncol=2 )
C <- matrix( c( c(1,1,2,4,5), c(4,4,0,1,2)), ncol=2 )
data <- data.frame(A,B)
 
plot( 0, type = "b", xlim = c(0,5),
     ylim = c(0,5) )
lines( A, col = "red" )
points( A, col = "yellow",
       pch = 16 )
lines( C, col = "green" )
points( C, col = "pink", pch = 15 )
lines( B, col = "blue" )
points( B, col = "violet", pch = 17 )
 
legend( x = "topright",
        legend = c("Red line, Yellow points","Blue line,
                  iolet points","Green line,Pink points"),
        col = c("red","blue","green"), lwd=1, lty=c(1,1,1),
        pch=c(NA,NA), cex = 0.5 )

输出:

通过重叠向图例添加点符号

现在我们将只重叠图例,符号,正如我们之前观察到的,线,是可见的线图例而不是点符号。

所以我们要做的是,将值分配给“pch”,我们可以根据用户选择从 1 到 25 不等,每个数字代表一个点形状,即 pch =1,这是一个空心圆,pch = 19(实心圆)、pch = 21(实心圆)等。

电阻

# data
A <- matrix( c( c(0,1,2,3,5), c(1,3,4,2,4)), ncol=2 )
B <- matrix( c( c(3,1,2,3,4), c(4,2,1,1,2)), ncol=2 )
C <- matrix( c( c(1,1,2,4,5), c(4,4,0,1,2)), ncol=2 )
data <- data.frame(A,B)
 
# plot
plot( 0, type = "b", xlim=c(0,5), ylim=c(0,5) )
lines( A, col = "red" )
points( A, col = "yellow", pch=16 )
lines( C, col = "green" )
points( C, col = "pink", pch=15 )
lines( B, col = "blue" )
points( B, col = "violet", pch=17 )
 
# legend
legend( x="topright",
        legend=c("Red line, Yellow points","Blue line,
                iolet points","Green line,Pink points"),
        col=c("red","blue","green"), lwd=1, lty=c(1,1,1),
        pch=c(NA,NA),cex=0.5 )
 
legend( x="topright",
        legend=c("Red line, Yellow points","Blue line,
                violet points","Green line,Pink points"),
        col=c("yellow","purple","pink"), lwd=1, lty=c(0,0,0),
        pch=c(16,15,17),cex=0.5 )

输出

.