ggplot: aes vs aes_string, or how to programmatically specify column names?

前端 未结 3 1323
别那么骄傲
别那么骄傲 2021-02-15 01:29

Let\'s assume we have the following data frame

data <- data.frame(time=1:10, y1=runif(10), y2=runif(10), y3=runif(10))

and we want to create

3条回答
  •  鱼传尺愫
    2021-02-15 02:20

    You could melt (thanks for reminding me of this function, rawr) all of your data into a few columns. For example, it could look like this:

    library(reshape2)    
    data2 <- melt(data, id = "time")
    head(data2)
    #    time variable       value
    # 1     1       y1 0.353088575
    # 2     2       y1 0.621565368
    # 3     3       y1 0.696031085
    # 4     4       y1 0.507112969
    # 5     5       y1 0.009560710
    # 6     6       y1 0.158993988
    ggplot(data2, aes(x = time, y = value, color = variable)) + geom_line()
    

    enter image description here

提交回复
热议问题