How to draw multiple Lines from csv in R

后端 未结 2 1381
南笙
南笙 2021-01-15 21:21

I just started using R and like to know how to plot a line . With one of my tool I am doing regression which generating csv files. FOrmat is as follows:

         


        
相关标签:
2条回答
  • 2021-01-15 21:50

    I'd probably use matplot if you want to use base R:

    #Fake data
    x <- data.frame(x = 1:100, y1 = rnorm(100), y2 = runif(100))
    #Plot
    matplot(x[,1], x[, -1], type="l", lty = 1)
    #Everyone needs a little legend love
    legend("topright", legend = colnames(x)[-1], fill=seq_along(colnames(x)[-1]))
    

    enter image description here

    Or, I'd use ggplot2

    library(ggplot2)
    library(reshape2)
    #Melt into long format with first column as the id variable
    x.m <- melt(x, id.vars = 1)
    #Plot it
    ggplot(x.m, aes(x, value, colour = variable)) +
      geom_line() +
      theme_bw()
    

    enter image description here

    That answer is remarkably similar to this one and several others that pop up as related on the right when you look at that question.

    0 讨论(0)
  • 2021-01-15 21:59
    yourData <- read.csv("yourCSV.csv")
    with(yourData, plot(X, Y, type = "l"))
    with(yourData, lines(X, Y1))
    with(yourData, lines(X, Y2))
    

    Also see ?abline.

    0 讨论(0)
提交回复
热议问题