首页 文章

使用ifelse时,使用ggplot2的3个变量(混合)的散点图在r中给出了错误的结果

提问于
浏览
0

enter image description here

试图使用ggplot2绘制三个不同变量的散点图 . 我希望每个变量都有自己的图标绘图形状和颜色 . 但是,代码给出了两个树形图是完全相同的,这是错误的!

## generate the data
  N=2000
  dim=dim
  U=runif(N, min=0,max=1)
  #set.seed(123)
  #U=runif(N, min=0,max=1)
  X = matrix(NA, nrow=N, ncol=3)
  for (i in 1:N){
    if(U[i] < 0.5){
      X[i,] <- rCopula(1,claytonCopula(1,dim=3)) ## must be lower tail
    } else if (U[i] < 0.3) {
      X[i,] <- rCopula(1,gumbelCopula(7,dim=3)) ## must be upper tail
    }else{
      X[i,] <- rCopula(1,frankCopula(2,dim=3))
    }
  }

此代码类似于此处使用的代码1How to get a scatter plot of mixture data with different shape and colour for each distribution? 1 . 但是,它仅适用于两个维度 .

###################################################################
df = data.frame(
  sh_of = ifelse(U < 0.3, "Gumbel", ifelse( U < 0.5, "Clayton", "Frank")),
  x = X[, 1],
  y = X[, 2],
  z = X[, 3]
)

ggplot(df, aes(x = x, y = y,z=z, colour = sh_of, shape = sh_of)) +
  geom_point()+scale_shape_manual(values=c(12,25,14)) + ggtitle("")+theme(plot.title = element_text(family = "Trebuchet MS", color="black", face="italic", size=15, hjust=0.5))

1 回答

  • 0

    ggplot2 忽略了 z 审美,并且出于某种原因添加 facet_wrap(~sh_of) . 如果你真的想使用 ggplot2 ,你可以对 z 维度进行分区并使用 facet_wrap(~z) 来绘制"slices" . 但要得到一个实际的3D情节,你需要另一个包 - 我会推荐 plotly3d scatterplot documentation)或 rglplot3d function

相关问题