Lanza
Lanza

Reputation: 597

How can I plot a lot of columns of a data frame with ggplot with the same color?

I have a data frame that looks like this:

  Time f1            f2
  6.04 0.0030113949 -2.816807e-03
  6.05 0.0030217415 -2.830386e-03
  6.06 0.0030320970 -2.843984e-03
  6.07 0.0030424615 -2.857600e-03
  6.08 0.0030528349 -2.871233e-03
  6.09 0.0030632171 -2.884885e-03
  6.10 0.0030736081 -2.898555e-03
  6.11 0.0030840079 -2.912242e-03

I want to plot f1 and f2 with ggplot with the same color, and their average with a different color, all of them on the same plot.

What I did:

 df <- melt(df ,  id.vars = 'Time', variable.name = 'f')
ggplot(df, aes(Time,value)) + geom_line(aes(colour = f))

But It plots every evry columns with different colors.

Upvotes: 0

Views: 1177

Answers (1)

Raphael K
Raphael K

Reputation: 2353

I'll take a shot.

install.packages('dplyr')
library(dplyr)
df <- mutate(df, f_mean = mean(f1 + f2))

ggplot(df, aes(x = Time, y = f1)) +
    geom_point(color = 'black') +
    geom_point(aes(x = Time, y = f2), color = 'black') +
    geom_point(aes(x = Time, y = f_mean), color = 'red')

You should be able to get to where you want to be with that code. Also, take a look at the ggplot2 cheat sheet.

Upvotes: 2

Related Questions