Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: how to color a graph by multiple variables

Tags:

r

ggplot2

I am fairly certain I have seen a solution for this somewhere, but as I have been unable to find it, here is my problem.

I have some time series data identified by multiple variables, I would like to be able to graph and differentiate color using multiple variables in ggplot2.

Sample data:

date <- c("2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC",
          "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC",
          "2016-06-01 UTC", "2016-04-01 UTC")
temp <- c(80.24018,  85.88911, 104.23125,  85.13571,  91.21129, 104.88333,  97.81116,
          107.40484, 121.03958,  87.91830)
id <- c("A","A","A","A","A","B","B","B","B","B")
location <- c("N","S","S","N","N","S","N","S","N","S")

df <- data.frame(date,temp,id,location)

My attempt at graphing

library(ggplot2)

ggplot(df) + 
  geom_line(aes(x=date,y=temp,colour=factor(location), group=interaction(location,id)))

Using this code it is only coloring by location. I would like to the lines to be colored by location and id.

like image 588
phaser Avatar asked Apr 25 '17 00:04

phaser


1 Answers

Two options:

library(ggplot2)

df <- data.frame(date = c("2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC"),
                 temp = c(80.24018,  85.88911, 104.23125,  85.13571,  91.21129, 104.88333,  97.81116, 107.40484, 121.03958,  87.91830),
                 id = c("A","A","A","A","A","B","B","B","B","B"),
                 location = c("N","S","S","N","N","S","N","S","N","S"))

df$date <- as.Date(df$date)    # parse dates to get a nicer x-axis

Map id to color and location to linetype:

ggplot(df, aes(date, temp, color = id, linetype = location)) + geom_path()

...or plot all interactions as different colors:

ggplot(df, aes(date, temp, color = id:location)) + geom_path()

like image 77
alistaire Avatar answered Sep 20 '22 15:09

alistaire