【问题标题】:3D plot in R errorR 错误中的 3D 绘图
【发布时间】:2019-01-31 05:50:27
【问题描述】:

我一直在尝试绘制我的数据的 3d 图,但我不知道如何克服一些错误。非常感谢任何帮助。

>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

我已经成功地使用这个来绘制热图:

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.*

但是,我想创建一个 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.

会不会是季度日期的性质弄乱了图表? (因为即使是热图也不会按季度显示日期。有什么提示吗?

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

【问题讨论】:

  • 你能发布dput(d1)的输出吗?
  • 完成。但是我删除了几行,以便我可以发布由于问题中的字符限制而进行的更改)
  • 确实需要一个矩阵,对应行和列。
  • 如果您有替代方法,切勿使用 rgl.* 函数。请改用surface3d()
  • @StéphaneLaurent 但即使我将d1 转换为矩阵它也不起作用。还是你的意思是别的?

标签: r plot 3d


【解决方案1】:

您上传的文件是 CSV 文件,而不是 dput 输出。但是你可以阅读它并像这样绘制它:

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")

这给出了以下情节:

【讨论】:

  • 哇,谢谢!最后一件小事……有可能让它像彩虹一样吗?我的意思是;是高值是红色,低值是蓝色,就像热图一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
相关资源
最近更新 更多