我如何图1000点到图形,用不同的点数是不同的颜色?

问题描述 投票:0回答:1

我一直在想有1 0之间的1000个值,如果他们满足特定的规则,这些规则在if语句,我想在一个特定的形状和颜色被提上了点。我曾尝试运行我的代码,但我得到的是在(0,0),用点的图表。

numOne <- sample(0:1, 1)
numTwo <- sample(0:1, 1)

plot(0,0, pch=5, col=5)

for(i in 999){
    a <- sample(0:1, 1)
    b <- sample(0:1, 1)

    if((a + b < 1) && (a - b < 0)){ lines(0, 0, pch=1, col=1) }
    if((a + b < 1) && (a - b < 0)){ lines(0, 0, pch=2, col=2) }
    if(!(a + b < 1) && (a - b < 0)){ lines(0, 1, pch=3, col=3) }
    if(!(a + b < 1) && (a - b < 0)){ lines(1, 0, pch=4, col=4) }

}
r
1个回答
1
投票

我不能完全肯定你想要做什么,但在这里就是我会去这样做什么,我认为你正在试图做的:

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

Here's the output

© www.soinside.com 2019 - 2024. All rights reserved.