我有一个这样的情节:
使用此代码创建的内容:
# 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
由于有这么多群体,传说真的没有用。 理想情况下,我想在图例中只有两个元素,一个用于group = 1,对于所有其他组(它们都应该具有相同的颜色)。有没有办法强迫这个?
答案 0 :(得分:1)
您可以定义一个只有两个值的新变量,但仍然根据原始组
绘制线条ggplot(data = df, aes(x = year, y = values, group = group,
colour = ifelse(group == 1, "1", "!1"))) +
geom_line() +
scale_colour_brewer("groups", palette="Set1")