【问题标题】:I have generated this data and I would like to plot it R我已经生成了这个数据,我想绘制它 R
【发布时间】:2021-05-21 20:01:16
【问题描述】:

我生成的数据如下:

set.seed(100)
n = 100
c1_prob = 0.8
X = matrix(0, nrow = n, ncol = 2)
y = matrix(0,nrow=n,ncol=1)
for (i in 1:n){
  if(runif(1) < c1_prob){
    X[i,] = mvrnorm(1,mu=c(2,2),Sigma=matrix(c(1,0,0,1),2,2))
    y[i] = 1;
  } else {
    X[i,] = mvrnorm(1,mu=c(-2,-2),Sigma=matrix(c(1,0,0,1),2,2))
    y[i] = 0;
  }
}

我想绘制 X,然后使用 y 中的 1 或 0 的类标签为点着色。我尝试创建一个合并 X 和 y 的数据框,然后绘制前两列,然后根据第三列(最初是 y)着色。

df = data.frame(cbind(X,y))
plot(df$X1, df$X2, col = df$X3)

但这不起作用,我想知道是否有其他方法可以做到这一点。具体来说,有没有一种方法可以绘制不需要我将两个矩阵合并到数据框中的数据。谢谢

【问题讨论】:

    标签: r plot classification


    【解决方案1】:

    试试 ggplots 这是一般格式。如果您想创建一个数据框并为其命名,那么只需输入列的标题而不是 x,y 即可

    library(ggplot2)
    your_data %>% ggplot(aes(X,Y, color = Y ))+geom_point()
    

    【讨论】:

    • 还有geom_line() geom_bar() 等等
    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    相关资源
    最近更新 更多