[英]How Do I Graph 1000 Points Onto A Graph, With Different Points Being Different Colors?
I have been trying to have a 1000 values between 1 & 0 and if they meet specific rules, which are in the if statements, I want the points to be put on in a specific shape and color. 我一直试图在1和0之间设置1000个值,并且如果它们满足if语句中的特定规则,我希望将这些点以特定的形状和颜色放置。 I have tried running my code, but all I get is a graph with a point at (0, 0).
我已经尝试运行代码,但是得到的只是一个点为(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) }
}
I'm not entirely certain what you're trying to do, but here's how I would go about doing what I think you're trying to do: 我不确定您要做什么,但是我将按照我认为您要做的事情去做:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.