2017-04-25 19 views
1

複数の変数によってグラフを着色する方法について説明します。ggplot2:私はこのどこかのソリューションを見てきましたかなり確信していますが、私はそれを見つけることができなかったとして、ここでは私の問題である

私は、複数の変数で識別されるいくつかの時系列データを持って、私はggplot2に複数の変数を使用して色をグラフ化して区別できるようにしたいと思います。

サンプルデータ:それは唯一の場所でカラーリングされ、このコードを使用して

library(ggplot2) 

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

をグラフで

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) 

私の試み。私は場所とIDによって色を付ける線にしたいと思います。

+1

'ggplot(DF、AES(as.Date(日付)、温度、色= ID、線種=場所))+ geom_path()'?または 'ggplot(df、aes(as.Date(date)、temp、color = id:location))+ geom_line()'ですが、それは読みにくいです。 – alistaire

答えて

4

つのオプション:色と線種にlocation

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 

地図id

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

...またはプロット異なる色など、すべての相互作用:

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

関連する問題

 関連する問題