简体   繁体   English

R错误中的3D图

[英]3D plot in R error

I have been trying to plot a 3d plot of my data but I cannot figure out how to overcome some errors. 我一直在尝试绘制数据的3D图,但是我无法弄清楚如何克服一些错误。 Any help is highly appreciated. 非常感谢您的帮助。

>head(d1) #produced through the melt function as seen below
     Date variable     value
1 2007 Q2    0.890 1.1358560
2 2007 Q3    0.890 1.1560433
3 2007 Q4    0.890 0.3747925
4 2008 Q1    0.890 0.3866533
5 2008 Q2    0.890 0.3872620
6 2008 Q3    0.890 0.3844887

I have successfully managed to plot a heatmap using this: 我已经成功地使用以下方法绘制了热图:

d1<-melt(mydata,id.vars = "Date")
P1 <- ggplot(data=d1, aes(x=Date, y=variable, fill=value)) + 
  geom_tile() +
  ggtitle("My heatmap") +scale_fill_gradientn(colors=colorRampPalette(c("lightgray","royalblue","seagreen","orange","red","brown"))(500),name="Variable") +
  labs(x = "Quarter",y="Alpha") +
  theme_bw()
ggplotly(P1)
*Don't know how to automatically pick scale for object of type yearqtr. Defaulting to continuous.*

在此处输入图片说明

However, I want to create a 3d plot. 但是,我想创建一个3D图。

open3d()
rgl.surface(x=d1$variable, y=d1$Date, 
            coords=c(1,3,2),z=d1$value, 
            color=colorzjet[ findInterval(d1$value, seq(min(d1$value), max(d1$value), length=100))] )
axes3d()
Error in rgl.surface(x = d1$variable, y = d1$Date, coords = c(1, 3, 2),  : 
'y' length != 'x' rows * 'z' cols


plot_ly(x=d1$Date,y=d1$variable,z=d1$value,type="surface",colors=colors)
Error: `z` must be a numeric matrix
I have tried to use as.matrix(apply(d1,2,as.numeric)), but this returns NAs to the date argument.

Could it be the nature of the Quarterly dates that messes up the graph? 可能是“季度”日期的性质弄乱了图表吗? (because even the heat map doesn't show the dates as Quarterly. Any tips? (因为即使热图也没有将日期显示为“每季度”。有什么提示吗?

dput(d1) output here: dput(d1) output dput(d1)输出在这里: dput(d1)输出

The file you uploaded is a CSV file, not dput output. 您上传的文件是CSV文件,而不是dput输出。 But you can read it and plot it like this: 但是您可以阅读并绘制它,如下所示:

d1csv <- read.csv("dput_output.csv")
year <- as.numeric(sub(" .*", "", d1csv$Date))
quarter <- as.numeric(sub(".*Q", "", d1csv$Date))

Date <- matrix(year + (quarter - 1)/4, 55)
variable <- matrix(d1csv$variable, 55)
value <- matrix(d1csv$value, 55)

persp3d(Date, variable, value, col = "red")

This gives the following plot: 这给出了以下图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM