将二维散点图热图扩展为三维

时间:2015-02-06 17:58:07

标签: r

现在我使用以下代码制作2-d热图:

library(ggplot2)
df=read.csv("C:/Users/sony/Desktop/Data.csv", header = TRUE)
qplot(df$knt, percent, data=df$percent, colour=df$avgTime,xlab=expression(paste('k'[s],"(N/m)")),ylab="Percentage of exiting agents") +
  scale_colour_continuous(name = "Average Time of exiting agents (sec)", low = "yellow", high = "black")

我希望转换为3维绘图,3维是名为df$k的列,(图表上的名称)z标签为k_e。所以可以这样做。我不想更改当前的显示格式,即值的标题。

我无法提供实际数据。但请检查以下内容:

数据:

qplot(mpg, wt, data = mtcars, colour = cyl, xlab = "MPG", ylab = "WT") +
  scale_colour_gradient2(name = "Cylinders", midpoint = median(mtcars$cyl),
                         low = "red", mid = "green", high = "black")

enter image description here

我希望如下所示: 3-d热图:(互动)

enter image description here

1 个答案:

答案 0 :(得分:4)

如果您想要交互式3D绘图,可以使用:

rgb.palette <- colorRampPalette(c("red", "green","black"), space = "rgb")
library(rgl)
with(mtcars,plot3d(wt, mpg, hp, col=rgb.palette(30)[as.numeric(cut(mtcars$qsec,breaks=30))], size=3,xlab="x-axis",ylab="y-axis",zlab="z-axis",main="title"))

我不知道制作颜色图例的功能。

如果您想要静态图,可以使用:

library(plot3D)
rgb.palette <- colorRampPalette(c("red", "green","black"), space = "rgb")
scatter3D(mtcars$wt,mtcars$mpg,mtcars$hp,colvar=mtcars$qsec,col=colorRampPalette(c("red", "green","black"))(200),pch=16,main="title",
          xlab="wt",ylab="mpg",zlab="hp",clab="Cyl",phi=20,theta=50,d=1,
          ticktype = "detailed",colkey = list(length = 0.5, width = 0.5, cex.clab = 0.75),
      bty = "g")

您可以使用phitheta更改绘图的角度,并通过删除bty="g"

删除网格线