如何根据ggplot2 scatterplot的数值阈值定义颜色组 [英] How do I define color groups based on numerical threshold values for ggplot2 scatterplot

查看:360
本文介绍了如何根据ggplot2 scatterplot的数值阈值定义颜色组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2个变量的数据集 x = event number& y =化验振幅。我试图在 ggplot2 中创建一个散点图,其中所有的点都是> 3000 以一种颜色着色,所有点< 3000 使用不同的颜色。

I have a data set that contains 2 variables x = event number & y = assay amplitude. I am trying to create a scatterplot in ggplot2 where all of the points that are > 3000 are colored in one color and all of the points < 3000 are in a different color.

我可以绘制图表并更改所有数据点的颜色,但无法弄清楚根据数值阈值定义颜色方案。

I can get the plot and change the color for all data points, but can't figure out how to define a color scheme based on the value threshold.

以下是我正在使用的数据示例:

Here is a sample of the data I'm using:

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))


推荐答案

你真的只需要为此做一个新的指标变量。正如@hrbrmstr所说, cut 是一种很好的通用方法(适用于任意数量的分割点)。

You really just need to do a new indicator variable for this. As @hrbrmstr says, cut is a good general way to do this (works for as many cutpoints as you want).

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()

这篇关于如何根据ggplot2 scatterplot的数值阈值定义颜色组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆