【问题标题】:Plot observations in same x-axis point which linked with id variable在与 id 变量相关联的同一 x 轴点绘制观测值
【发布时间】:2018-06-07 13:50:09
【问题描述】:

我需要帮助。这是我的数据库的视图:

482 940 914  1  
507 824 1042 2   
514 730 1450 3  
477 595 913  4  

我的目标是在每行 x 轴的同一点上绘制。
示例:
在1 (=x) 我想绘制482、940 和914
在2 (=x) 中,我想绘制507、824 和1042。

所以每个 x 轴点垂直三个点。

【问题讨论】:

    标签: r plot rstudio


    【解决方案1】:

    以可重现的方式共享数据是个好主意 - 我正在使用 readClipboard 将复制的向量读入 R。无论如何,这是一个快速的答案:

    x <- as.numeric(unlist(strsplit(readClipboard(), " ")))
    

    这使它成为一个数字向量。我们现在需要根据您提供的描述进行分组。我正在使用matrix 来实现这一点,然后将转换为data.frame 以使用ggplot2 进行绘图:

    m <- matrix(x, ncol = 4, byrow = T)
    > m
         [,1] [,2] [,3] [,4]
    [1,]  482  940  914    1
    [2,]  507  824 1042    2
    [3,]  514  730 1450    3
    [4,]  477  595  913    4
    
    df <- as.data.frame(m)
    # Assign names to the data.frame
    names(df) <- letters[1:4]
    
    > df
        a   b    c d
    1 482 940  914 1
    2 507 824 1042 2
    3 514 730 1450 3
    4 477 595  913 4
    

    获取剧情:

    library(ggplot2)
    ggplot(df, aes(x = d)) + 
        geom_point(aes(y = a), color = "red") + 
        geom_point(aes(y = b), color = "green")  + 
        geom_point(aes(y = c), color = "blue")
    

    输出

    您可以使用ggtitle 和xlab 等来更改情节标签并添加图例。

    希望这有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      相关资源
      最近更新 更多