用向量值绘制图形
Plot graph with values of vectors
我想在图形中可视化向量的元素。我想生成一个具有特定 x 轴和 y 轴的图形,然后将我的向量值作为点放入图形中。我还想为不同向量的值使用不同的颜色。我该怎么做?
例如:我在向量 A 中有 10 个元素,我想将这些元素放入图中。向量 A 的第一个元素具有 y 值 A[1] 和 x 值 1。向量 A 的第二个元素具有 y 值 A[2] 和 x 值 2。与向量 B 相同。
vec1 = 1:10
vec2 = 1:10
for(idx in 1:10){
vec1[idx] = runif(1, min=0, max=100)
vec2[idx] = runif(1, min=0, max=100)
}
plot(vec1 and vec2) // How do I do this?
vec1 的 dput 输出:c(81.9624423747882, 45.583715592511, 56.2400584807619, 8.25600677635521, 82.0227505406365, 45.6240070518106, 68.7916911672801, 94.491201499477, 22.0095717580989, 4.29550902917981)
vec2 的 dput 输出:c(29.5684755546972, 68.0154771078378, 52.2058120695874, 2.48502977192402, 91.9532125117257, 24.7736480785534, 66.5003522532061, 79.014728218317, 47.9641782585531, 20.5593338003382)
我想知道或猜测您是否正在瞄准基本绘图功能箭头提供的功能?这是 ?arrows 页面中的示例:
x <- stats::runif(12); y <- stats::rnorm(12)
i <- order(x, y); x <- x[i]; y <- y[i]
plot(x,y, main = "arrows(.)" )
## draw arrows from point to point :
s <- seq(length(x)-1) # one shorter than data
arrows(x[s], y[s], x[s+1], y[s+1], col = 1:3)
如果您想从原点开始绘制每个向量(由“箭头”表示),则为:
x <- stats::runif(12); y <- stats::rnorm(12)
# ordering not needed this time
plot(x,y, main = "arrows(.)", xlim=c(0, max(x)) # to let origin be seen)
## draw arrows from origin to point :
s <- length(x) # one shorter than data
arrows(rep(0,s), rep(0,s), x, y, col = 1:3)
开始于
vec1 = 1:10
vec2 = 1:10
for(idx in 1:10){
vec1[idx] = runif(1, min=0, max=100)
vec2[idx] = runif(1, min=0, max=100)
}
plot(vec1 and vec2) // How do I do this?
试试这个:
plot( 1:20, c(vec1,vec2) , col=rep(1:2,10) # just points
lines( 1:20, c(vec1,vec2) ) # add lines
# if you wanted the same x's for both sequences the first argument could be
# rep(1:10, 2) instead of 1:20
注意:您的设置代码可能只有两行(没有循环):
vec1 = runif(10, min=0, max=100)
vec2 = runif(10, min=0, max=100)
我认为最简单的是创建一个数据框,这通常是大多数函数在 R 中所期望的:
library(tidyverse)
vec1 = 1:10
vec2 = 1:10
for(idx in 1:10){
vec1[idx] = runif(1, min=0, max=100)
vec2[idx] = runif(1, min=0, max=100)
}
df <- data.frame(order = 1:10, vec1, vec2) %>%
pivot_longer(!order, names_to = "color", values_to = "value")
plot(df$order, df$value, col = c("red","blue")[df$color %>% as.factor()])
我想在图形中可视化向量的元素。我想生成一个具有特定 x 轴和 y 轴的图形,然后将我的向量值作为点放入图形中。我还想为不同向量的值使用不同的颜色。我该怎么做?
例如:我在向量 A 中有 10 个元素,我想将这些元素放入图中。向量 A 的第一个元素具有 y 值 A[1] 和 x 值 1。向量 A 的第二个元素具有 y 值 A[2] 和 x 值 2。与向量 B 相同。
vec1 = 1:10
vec2 = 1:10
for(idx in 1:10){
vec1[idx] = runif(1, min=0, max=100)
vec2[idx] = runif(1, min=0, max=100)
}
plot(vec1 and vec2) // How do I do this?
vec1 的 dput 输出:c(81.9624423747882, 45.583715592511, 56.2400584807619, 8.25600677635521, 82.0227505406365, 45.6240070518106, 68.7916911672801, 94.491201499477, 22.0095717580989, 4.29550902917981)
vec2 的 dput 输出:c(29.5684755546972, 68.0154771078378, 52.2058120695874, 2.48502977192402, 91.9532125117257, 24.7736480785534, 66.5003522532061, 79.014728218317, 47.9641782585531, 20.5593338003382)
我想知道或猜测您是否正在瞄准基本绘图功能箭头提供的功能?这是 ?arrows 页面中的示例:
x <- stats::runif(12); y <- stats::rnorm(12)
i <- order(x, y); x <- x[i]; y <- y[i]
plot(x,y, main = "arrows(.)" )
## draw arrows from point to point :
s <- seq(length(x)-1) # one shorter than data
arrows(x[s], y[s], x[s+1], y[s+1], col = 1:3)
如果您想从原点开始绘制每个向量(由“箭头”表示),则为:
x <- stats::runif(12); y <- stats::rnorm(12)
# ordering not needed this time
plot(x,y, main = "arrows(.)", xlim=c(0, max(x)) # to let origin be seen)
## draw arrows from origin to point :
s <- length(x) # one shorter than data
arrows(rep(0,s), rep(0,s), x, y, col = 1:3)
开始于
vec1 = 1:10
vec2 = 1:10
for(idx in 1:10){
vec1[idx] = runif(1, min=0, max=100)
vec2[idx] = runif(1, min=0, max=100)
}
plot(vec1 and vec2) // How do I do this?
试试这个:
plot( 1:20, c(vec1,vec2) , col=rep(1:2,10) # just points
lines( 1:20, c(vec1,vec2) ) # add lines
# if you wanted the same x's for both sequences the first argument could be
# rep(1:10, 2) instead of 1:20
注意:您的设置代码可能只有两行(没有循环):
vec1 = runif(10, min=0, max=100)
vec2 = runif(10, min=0, max=100)
我认为最简单的是创建一个数据框,这通常是大多数函数在 R 中所期望的:
library(tidyverse)
vec1 = 1:10
vec2 = 1:10
for(idx in 1:10){
vec1[idx] = runif(1, min=0, max=100)
vec2[idx] = runif(1, min=0, max=100)
}
df <- data.frame(order = 1:10, vec1, vec2) %>%
pivot_longer(!order, names_to = "color", values_to = "value")
plot(df$order, df$value, col = c("red","blue")[df$color %>% as.factor()])