Plotting multiple lines from a data frame in R

前端 未结 3 665
孤街浪徒
孤街浪徒 2020-12-18 04:23

I am building an R function to plot a few lines from a data table, I don\'t understand why this is not working?

data = read.table(path, header=TRUE);
plot(da         


        
相关标签:
3条回答
  • 2020-12-18 04:37

    Have a look at the ggplot2 package

    library(ggplot2)
    library(reshape)
    data <- data.frame(time = seq(0, 23), noob = rnorm(24), plus = runif(24), extra = rpois(24, lambda = 1))
    Molten <- melt(data, id.vars = "time")
    ggplot(Molten, aes(x = time, y = value, colour = variable)) + geom_line()
    

    enter image description here

    0 讨论(0)
  • 2020-12-18 04:53

    You don't need to load any package of for or apply, just simply use the matplot function built in R... Each column of a table will be a line in your graph (or symbols if you prefer).

    0 讨论(0)
  • 2020-12-18 05:02

    Or with base:

    data <- data.frame(time = seq(0, 23), noob = rnorm(24), plus = runif(24), extra = rpois(24, lambda = 1))
    plot(extra ~ time, 
      data = data, 
      type = "l", 
      ylim = c(min(data[ ,-1]), max(data[ ,-1])),
      ylab = "value")
    lines(data$time, data$plus, col = "steelblue")
    lines(data$time, data$noob, col = "pink")
    
    0 讨论(0)
提交回复
热议问题