【问题标题】:Plot graph with values of vectors绘制带有向量值的图形
【发布时间】:2021-08-09 23:27:38
【问题描述】:

我想在图表中可视化我的向量的元素。我想生成一个具有特定 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 的输入输出:c(81.9624423747882, 45.583715592511, 56.2400584807619, 8.25600677635521, 82.0227505406365, 45.6240070518106, 68.7916911672801, 94.491201499477, 22.0095717580989, 4.29550902917981)

vec2 的输入输出:c(29.5684755546972, 68.0154771078378, 52.2058120695874, 2.48502977192402, 91.9532125117257, 24.7736480785534, 66.5003522532061, 79.014728218317, 47.9641782585531, 20.5593338003382)

【问题讨论】:

  • 你试过什么? plot(A) 有什么问题?
  • @KonradRudolph 我希望在一张图中可视化多个向量。我不知道该怎么做,因为plot(vec) 只在向量上绘图。
  • 请阅读此处提供的文档和示例,其中包含执行此操作的代码。
  • 嗨@abcdefghi019283,您能否添加一个最小的可重现示例。如果您不确定如何执行此操作,请查看此link。您的数据的简化版本以及您已经尝试过的方法。
  • @KonradRudolph 你有文档或这些示例的链接吗?我没有发现任何有用的东西。

标签: r plot vector graph


【解决方案1】:

开始

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)

【讨论】:

    【解决方案2】:

    我认为最简单的方法是创建一个数据框,这通常是大多数函数在 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()])
    
    

    【讨论】:

    • 我明白你在说什么@Konrad Rudolph,如果我在重新阅读问题后理解正确,我相信这就是我回答的第二部分正在做的事情(尽管我确实称之为@987654324 @)。我编辑了答案以保持这一点。还是我错过了什么?
    【解决方案3】:

    我想知道或猜测您的目标是否是基础绘图功能箭头提供的功能?这是 ?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)
    

    【讨论】:

    • 不,我只想在一个图表中绘制每个向量的每个值。没有箭头或手段,只有简单的价值观。 plot(vec) 为一个向量完成这项工作。但是我有两个向量,我不知道如何将这些值放入同一个图中。
    猜你喜欢
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多