简体   繁体   English

如何在图形上将1000个点绘制成不同颜色的点?

[英]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.

相关问题 我如何使用 R 在 R 中随机使用 3 个不同的 colors 为图形点着色 - How can i color points of a graph randomly with 3 different colors in R using R basic only 我在 3 个不同时间点收集了 7 个不同的病毒浓度数据点。 我如何在 R 中用误差线绘制这个图? - I have 7 different data points for virus concentration collected at 3 different time points. How do I graph this with error bars in R? R:根据高级标准绘制具有不同颜色点的图形 - R: Plotting a graph with different colors of points based on advanced criteria 如何使 r 中图表上的一系列点具有不同的颜色 - How to make a range of points on a graph i n r a different colour 如何在rPlot中为不同的点设置不同的颜色? - How to set different colors for different points in rPlot? 用3种不同颜色绘制点 - Plot points with 3 different colors R:带有不同颜色的点和线的图例(对于相同的图例项) - R: legend with points and lines being different colors (for the same legend item) 如何在具有两种不同颜色的晶格上绘制图形 - how to plot a graph on lattice with two different colors 使用R将图表和颜色添加到图表上的不同点 - adding labels and colour to different points on a graph using R 在R中的图形上绘制多组不同的数据点 - plotting multiple sets of different data points on a graph in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM