R:在线图中,您如何控制用于每个点(而不是每个系列)的字符?
R: in a line plot, how do you control the character used for each point (rather than for each series)?
我正在用几个 lines/series 绘制线图。我想 line/series.
中的每个点都有不同的字符
我目前的代码是这样的:
x <- rep(1:5, 4) * rep(rnorm(5), each = 4)
x <- matrix(x, 5, 4)
matplot(x, type = "b", pch = 1:4)
但是,'pch' 只允许您更改用于一个系列中所有点的字符。有没有办法单独控制每个点?
非常感谢!
如果你尝试一个简单的例子:plot(x=c(1,2,3,4),y=c(5,5,5,5),type="b",pch=c(1,2,3,4))
你会看到每个点都有不同的pch。
因此,如果您希望每个点和每条线都有不同的 pch,请尝试制作一个矩阵,其中线将代表每条线的 pch。
@mathematical.coffee 回答后我阅读了 ?matplot。
不幸的是,你不能为每个点使用不同的 pch,但对于 matplot 中的每个图。
plot
允许系列中每个点有不同的 pch
; matplot
没有(它只允许每个系列有一个不同的)。
所以你必须使用 plot
/points
/lines
而不是 matplot
.
例如
xs <- 1:nrow(x)
cols <- c('blue', 'red', 'black', 'green', 'yellow')
# set up empty plot with the right limits
matplot(x, type='n')
for (i in 1:ncol(x))
lines(xs, x[, i], type='b', pch=(i - 1)*(1:nrow(x))+1, col=cols[i])
我正在用几个 lines/series 绘制线图。我想 line/series.
中的每个点都有不同的字符我目前的代码是这样的:
x <- rep(1:5, 4) * rep(rnorm(5), each = 4)
x <- matrix(x, 5, 4)
matplot(x, type = "b", pch = 1:4)
但是,'pch' 只允许您更改用于一个系列中所有点的字符。有没有办法单独控制每个点?
非常感谢!
如果你尝试一个简单的例子:plot(x=c(1,2,3,4),y=c(5,5,5,5),type="b",pch=c(1,2,3,4))
你会看到每个点都有不同的pch。 因此,如果您希望每个点和每条线都有不同的 pch,请尝试制作一个矩阵,其中线将代表每条线的 pch。
@mathematical.coffee 回答后我阅读了 ?matplot。 不幸的是,你不能为每个点使用不同的 pch,但对于 matplot 中的每个图。
plot
允许系列中每个点有不同的 pch
; matplot
没有(它只允许每个系列有一个不同的)。
所以你必须使用 plot
/points
/lines
而不是 matplot
.
例如
xs <- 1:nrow(x)
cols <- c('blue', 'red', 'black', 'green', 'yellow')
# set up empty plot with the right limits
matplot(x, type='n')
for (i in 1:ncol(x))
lines(xs, x[, i], type='b', pch=(i - 1)*(1:nrow(x))+1, col=cols[i])