为R中散点图上不同间隔的点赋予不同的颜色

Giving different colors to the dots that are in different intervals on a scatter plot in R

我是 R 的初学者,想为散点图中的点赋予不同的颜色,在我的 x 轴上介于 0 -> 0.4、0.4 ->0.8 和 0.8 -> 1 之间。 我在谷歌上搜索了很多,但找不到解决方案的提示。

我正在使用此代码进行绘图:

ggplot(xlim=1, ylim=1,)+geom_point(data=df,aes(x1,y1))+
  geom_circle(aes(x0 = x0, y0 = y0 ,r = r,colour=cb), data = circ_kv) +
  coord_fixed(xlim=c(0,1),ylim=c(0,1))

非常感谢所有帮助或提示!

this is the scatter plot i am working with

是这样的吗?

library(tidyverse)

set.seed(1337)

data <- tibble(
  x = runif(300),
  y = runif(300)
)
  
data %>%
  mutate(
    # distance to origin
    r = sqrt(x**2 + y**2),
    r_group = case_when(
      r < 0.4 ~ "group 1",
      r < 0.8 ~ "group 2",
      r < 1.0 ~ "group 3"
    )
  ) %>%
  ggplot(aes(x,y, color = r_group)) +
    geom_point() +
    coord_fixed()

reprex package (v2.0.1)

于 2021-12-09 创建