我
试图使用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))
}
}
此代码类似于此处使用的代码1:How 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 回答
ggplot2
忽略了z
审美,并且出于某种原因添加facet_wrap(~sh_of)
. 如果你真的想使用ggplot2
,你可以对z
维度进行分区并使用facet_wrap(~z)
来绘制"slices" . 但要得到一个实际的3D情节,你需要另一个包 - 我会推荐plotly
(3d scatterplot documentation)或rgl
(plot3d function)