本内容为【科研私家菜】R可视化之美之科研绘图系列课程
快来收藏关注【科研私家菜】
01 三维散点图/3D散点图
三维散点图就是在由3个变量确定的三维空间中研究变量之间的关系,由于同时考虑了3个变量,常常可以发现在两维图形中发现不了的信息。
library(plot3D)
library(scales)
library(RColorBrewer)
library(fields)
df<-read.csv("ThreeD_Scatter_Data.csv",header=T)
pmar <- par(mar = c(5.1, 4.1, 4.1, 6.1))
with(df, scatter3D(x = mph, y = Gas_Mileage, z = Power, #bgvar = mag,
pch = 21, cex = 1.5,col="black",bg="#F57446",
xlab = "0-60 mph (sec)",
ylab = "Gas Mileage (mpg)",
zlab = "Power (kW)",
zlim=c(40,180),
ticktype = "detailed",bty = "f",box = TRUE,
#panel.first = panelfirst,
theta = 60, phi = 20, d=3,
colkey = FALSE)#list(length = 0.5, width = 0.5, cex.clab = 0.75))
)
效果如下:
02
index <- ceiling(((prc <- 0.7 * df$Weight/ diff(range(df$Weight))) - min(prc) + 0.3)*100)
for (i in seq(1,length(index)) ){
prc[i]=colormap[index[i]]
}
pmar <- par(mar = c(5.1, 4.1, 4.1, 6.1))
with(df, scatter3D(x = mph, y = Gas_Mileage, z = Power, #bgvar = mag,
pch = 21, cex = 1.5,col="black",bg=prc,
xlab = "0-60 mph (sec)",
ylab = "Gas Mileage (mpg)",
zlab = "Power (kW)",
zlim=c(40,180),
ticktype = "detailed",bty = "f",box = TRUE,
#panel.first = panelfirst,
theta = 60, phi = 20, d=3,
colkey = FALSE)#list(length = 0.5, width = 0.5, cex.clab = 0.75))
)
colkey (col=colormap,clim=range(df$Weight),clab = "Weight", add=TRUE, length=0.5,side = 4)
效果如下:
index <- ceiling(((prc <- 0.7 * df$Weight/ diff(range(df$Weight))) - min(prc) + 0.3)*100)
for (i in seq(1,length(index)) ){
prc[i]=colormap[index[i]]
}
pmar <- par(mar = c(5.1, 4.1, 4.1, 6.1))
with(df, scatter3D(x = mph, y = Gas_Mileage, z = Power, #bgvar = mag,
pch = 21, cex = rescale(df$Weight, c(.5, 5)),col="black",bg=prc,
xlab = "0-60 mph (sec)",
ylab = "Gas Mileage (mpg)",
zlab = "Power (kW)",
zlim=c(40,180),
ticktype = "detailed",bty = "f",box = TRUE,
theta = 60, phi = 20, d=3,
colkey = FALSE)
)
#colkey (col=colormap,clim=range(df$Weight),clab = "Weight", add=TRUE, length=0.5,side = 4)
breaks<-round(seq(500,2000,length.out=4),3)
legend_index <- ceiling(((legend_prc <- 0.7 *breaks/ diff(range(breaks))) - min(legend_prc) + 0.3)*100)
for (i in seq(1,length(legend_index)) ){
legend_prc[i]=colormap[legend_index[i]]
}
legend("right",title = "Weight",legend=breaks,pch=21,
pt.cex=rescale(breaks, c(.5, 5)),y.intersp=1.6,
pt.bg = legend_prc,bg="white",bty="n")
pmar <- par(mar = c(5.1, 4.1, 4.1, 6.1))
with(iris, scatter3D(x = Sepal.Length, y = Sepal.Width, z = Petal.Length,pch = 21,
cex = rescale(iris$quan, c(.5, 4)),col="black",bg=colormap[iris$quan],
xlab = "Sepal.Length",
ylab = "Sepal.Width",
zlab = "Petal.Length",
ticktype = "detailed",bty = "f",box = TRUE,
theta = 30, phi = 15, d=2,
colkey = FALSE)
)
breaks =1:6
legend("right",title = "Weight",legend=breaks,pch=21,
pt.cex=rescale(breaks, c(.5, 4)),y.intersp=1.6,
pt.bg = colormap[1:6],bg="white",bty="n")
效果如下:
参考资料
《R语言数据可视化之美》
关注R小盐,关注科研私家菜(溦❤工众號: SciPrivate),有问题请联系R小盐。让我们一起来学习 R可视化之美之科研绘图