Constructing a line graph using ggplot2

后端 未结 2 768
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 07:47

I need to plot three lines (onto a single graph) each of which represents one lab team\'s data (two variables / team). Ideally, the graph should look aesthetically pleasing (hen

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

    The key thing is to organize your data before plotting so as to have a factor column in the data frame indicating a separate line for each set of x, y values. For example:

    set.seed(1)
    df1 <- data.frame(e1 = sort(runif(5, 0.05, 0.25)),
                      e2 = sort(runif(5, 0.05, 0.25)),
                      e3 = sort(runif(5, 0.05, 0.25)),
                      t1 = sort(runif(5, 1, 100)),
                      t2 = sort(runif(5, 1, 100)),
                      t3 = sort(runif(5, 1, 100))
                      )
    ### reshape this to give a column indicating group
    df2 <- with(df1,
            as.data.frame(cbind( c(t1, t2, t3),
                                c(e1, e2, e3),
                                rep(seq(3), each=5) )
                          ))
    colnames(df2) <- c("temp","activity","team")
    df2$team <- as.factor(df2$team)
    

    Then

    library(ggplot2)
    ggplot(df2, aes(x=temp, y=activity, col=team)) + geom_line()
    

    giving:enter image description here

    0 讨论(0)
  • 2021-01-21 08:27

    I'd do something like:

    library(ggplot2)
    ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(cyl))) + geom_line()
    

    enter image description here

    If you want more specific advice, I would suggest you expand your example, and include some example data, and provide more details regarding what the visualisation should tell.

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