2016-12-13 7 views
0

私はこのようなプロットを持っている:非常に多くのグループがあるので多くのグループとの伝説を制御する方法

# Make data: 
set.seed(42) 
n <- 1000 
df <- data.frame(values = sample(0:5, size = n, replace = T, prob = c(9/10, rep(0.0167,5))), 
       group = rep(1:100, each = 10), 
       fill2 = rep(rnorm(10), each = 100), 
       year = rep(2001:2010, times = 100) 
       ) 
df$values <- ifelse(df$year %in% 2001:2007 == T, 0, df$values) 

# Plot 
require(ggplot2) 
p <- ggplot(data = df, aes(x = year, y = values, colour = as.factor(group))) + geom_line() 
p 

は、伝説がある:このコードを使用して作成された

enter image description here

本当に役に立たない。 理想的には、凡例には2つの要素、1つはグループ= 1、他のすべてのグループはすべて同じ色でなければなりません。これを強制する方法はありますか?

答えて

1

あなただけの2つの値を持つ新しい変数を定義しますが、まだ彼らの元のグループに応じたラインをプロットすることができ、

ggplot(data = df, aes(x = year, y = values, group = group, 
         colour = ifelse(group == 1, "1", "!1"))) + 
    geom_line() + 
    scale_colour_brewer("groups", palette="Set1") 
関連する問題