【问题标题】:How to create a line graph with 3 column data set?如何创建具有 3 列数据集的折线图?
【发布时间】:2020-10-07 00:53:16
【问题描述】:

我有一个来自 csv 的三列数据源。我已经使用 read.csv 将其导入到 R 中。数据集如下所示。

      LRV Variance      Date
   1: 101    4.541  9/1/2020
   2: 101    0.000  9/2/2020
   3: 101    8.992  9/3/2020
   4: 101   23.233  9/4/2020
   5: 101    6.347  9/5/2020
  ---                       
1696: 150    4.516 9/30/2020
1697: 150    0.000 10/1/2020
1698: 150    0.000 10/2/2020

我正在尝试生成一个折线图,其中 x 轴作为日期,Y 轴作为方差,以及代表每个 LRV 的绘图点/线。

我尝试使用 ggplot 来做到这一点。

ggplot(mydata, aes(Date, Variance, colour = LRV)) + geom_point() + geom_path()

但这最终会在每天而不是每个 LRV 内绘制一条线。 Here's the output

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    改用geom_line() 尝试这种方法(我使用了您的数据,所以有空日期)。这里使用ggplot2的代码:

    library(ggplot2)
    #Format date
    df$Date <- as.Date(df$Date,'%m/%d/%Y')
    #Code
    ggplot(df, aes(Date,Variance, color=factor(LRV),group=factor(LRV)))+
      geom_line()+
      scale_x_date(date_labels = '%Y-%m_%d')
    

    输出:

    使用的一些数据:

    #Data
    df <- structure(list(LRV = c(101, 101, 101, 101, 101, 150, 150, 150, 
    151, 151, 151), Variance = c(4.541, 0, 8.992, 23.233, 6.347, 
    4.516, 0, 0, 6, 9, 15), Date = c("09/01/2020", "09/02/2020", 
    "09/03/2020", "09/04/2020", "09/05/2020", "09/30/2020", "10/01/2020", 
    "10/02/2020", "09/30/2020", "10/01/2020", "10/02/2020")), row.names = c(NA, 
    -11L), class = "data.frame")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 2019-12-01
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多