我有一个facet_grid,并希望根据增长变量的符号对不同行上的点进行着色。出于某种原因,我在大多数面板中得到2条线。从图表中可以看出它无法正常工作。
df2$sign <- paste(sign(df2[df2$variable %in% c('growth'), 'value']))
gg <- ggplot(df2, aes(x = date, y = value, colour = sign)) + geom_line() + facet_grid(variable ~ ., scales = 'free_y')
gg <- gg + scale_fill_manual(values = c('1' = "green", '-1' = "red", 'NA' = 'black'))
更新,整个输出可能太多,这里是一个块
> dput(df2[sort(sample(1:nrow(df2), 20)), ])
structure(list(date = structure(c(113, 204, 330, 442, 764, 834,
274, 582, 239, 316, 533, 57, 680, 715, 813, 120, 162, 260, 582,
785), class = "Date"), variable = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L
), .Label = c("x", "trline", "abovtrend", "growth", "growthpc"
), class = "factor"), value = c(13.3161181999425, 16.1064371699508,
17.4335499999019, 15.7080705228661, 21.2452347312702, 24.1828936950835,
15.8272720823308, 19.1001953187372, 0.716627289232031, 0.618404918297323,
-0.219587860370801, NA, 4.20745119442357, 1.15183152074574, 0.978670728352593,
NA, NA, NA, 0.808835828791911, 0.179073426736106), sign = c("NA",
"NA", "NA", "-1", "1", "1", "NA", "1", "NA", "NA", "1", "NA",
"1", "1", "1", "NA", "NA", "NA", "1", "1")), .Names = c("date",
"variable", "value", "sign"), row.names = c(17L, 30L, 48L, 64L,
110L, 120L, 160L, 204L, 275L, 286L, 317L, 369L, 458L, 463L, 477L,
498L, 504L, 518L, 564L, 593L), class = "data.frame")
答案 0 :(得分:0)
就像@hrbrmstr在他的评论中所说的那样,我们真的需要这里的数据给出一个明确的答案,但我相信你得到的结果是因为你实际上还没有为你的情节添加任何分数。你为你的线条着色。这是你想要的吗?
df <- data.frame(
x = 1:5,
y1 = c(1, 2, -2, -3, 5)
)
df$color <- ifelse(is.na(df$y1), "NA",ifelse(df$y1 < 0,"-1","1"))
ggplot(df, aes(x, y1)) + geom_point(aes(color=color),size=4) + geom_line() + theme_minimal()
此颜色指向0以下为红色,上方为蓝色。它也很容易改变颜色。
答案 1 :(得分:0)