use*_*134 5 r colors scatter-plot ggplot2 threshold
我的数据集包含2个变量x=事件编号和y=测定幅度.我正在尝试创建一个散点图,ggplot2其中所有的点> 3000都用一种颜色着色,所有的点< 3000都是不同的颜色.
我可以获得绘图并更改所有数据点的颜色,但无法弄清楚如何根据值阈值定义颜色方案.
以下是我正在使用的数据示例:
dat <- data.frame(x=c(399, 16022, 14756, 2609, 1131, 12135,
7097, 12438, 12604, 14912, 11042,
14024, 7033, 4971, 15533, 4507, 4627,
12600, 7458, 14557, 3999, 3154, 6073),
y=c(3063.40137, 3687.42041, 3911.856,
4070.91748, 4089.99561, 4095.50317,
4159.899, 4173.117, 4177.78955,
4186.46875, 4201.874, 4272.022,
638.615, 649.8995, 668.8346,
688.754639, 711.92, 712.689636,
721.1352, 737.841, 741.0727,
755.2549, 756.730652))
Run Code Online (Sandbox Code Playgroud)
你真的只需要为此做一个新的指标变量.正如@hrbrmstr所说,这cut是一个很好的通用方法(适用于任意数量的切割点).
dat$col <- cut(dat$y,
breaks = c(-Inf, 3000, Inf),
labels = c("<=3000", ">3000"))
ggplot(dat, aes(x = x, y = y, color = col)) +
geom_point()
Run Code Online (Sandbox Code Playgroud)