R 如何将1000个点绘制到图形上,不同的点具有不同的颜色?

R 如何将1000个点绘制到图形上,不同的点具有不同的颜色?,r,R,我一直在尝试让1000个值介于1和0之间,如果它们符合特定的规则(在if语句中),我希望这些点以特定的形状和颜色显示。我已经试着运行我的代码,但我得到的只是一个点位于(0,0)的图 numOne我不完全确定你想做什么,但以下是我如何着手做我认为你想做的事情: library(dplyr) library(ggplot2) # create a dataframe with random x and y values data <- data.frame(x = runif(n = 100

我一直在尝试让1000个值介于1和0之间,如果它们符合特定的规则(在if语句中),我希望这些点以特定的形状和颜色显示。我已经试着运行我的代码,但我得到的只是一个点位于(0,0)的图


numOne我不完全确定你想做什么,但以下是我如何着手做我认为你想做的事情:

library(dplyr)
library(ggplot2)

# create a dataframe with random x and y values
data <- data.frame(x = runif(n = 1000, min = 0, max = 1),
                   y = runif(n = 1000, min = 0, max = 1))

# add a new column to the data identifying the group
data <- data %>% 
           mutate(group = if_else(condition = (x + y < 1) & (x - y < 0), 
                                  true = 'a', 
                                  false = 'b'))

# plot the data with a different shape and color for each group
ggplot(data, 
       aes(x = x,
           y = y,
           color=group,
           shape=group)) +
   geom_point()
库(dplyr)
图书馆(GG2)
#创建具有随机x和y值的数据帧

数据此代码给出了一个错误
b中的错误:未找到对象“b”
a和b在哪里定义?抱歉!x实际上是a,y是b。如果您试图绘制点,为什么要使用
功能而不是
?您有四个
if
语句。前两个具有完全相同的条件,数字3和4具有相同的条件。
library(dplyr)
library(ggplot2)

# create a dataframe with random x and y values
data <- data.frame(x = runif(n = 1000, min = 0, max = 1),
                   y = runif(n = 1000, min = 0, max = 1))

# add a new column to the data identifying the group
data <- data %>% 
           mutate(group = if_else(condition = (x + y < 1) & (x - y < 0), 
                                  true = 'a', 
                                  false = 'b'))

# plot the data with a different shape and color for each group
ggplot(data, 
       aes(x = x,
           y = y,
           color=group,
           shape=group)) +
   geom_point()