rrayshader

rayshader 3D ggplots - issue with z values


I am trying to exploit some features of R to plot some crypto data. I am trying to use some examples found at https://www.tylermw.com/3d-ggplots-with-rayshader/ but I am having an issue since on the Z axis I do not get any data. I have on the x,-axis time and price and I want to plot the volume on the z-axis (third column of the file). At the moment I do not see the z values of the volume. I am sending the R script that I am using:

library(tidyverse)
library(viridis)
library(rayshader)
library(ggplot2)

dec = "."

btcUSDT <- read.csv("test orderbook/data/BTC_USDT 20220720_r.txt",header=TRUE,sep=";",dec=dec,strip.white = TRUE)
head(btcUSDT)

class(btcUSDT$time)
class(btcUSDT$price)
class(btcUSDT$volume)

btcUSDT %>%
ggplot(aes(x = time, y = price) ) +
geom_tile(aes(fill = volume),size=1,color="black") +
scale_x_continuous("Time") +
scale_y_discrete("Price") +
ggtitle("USDT order book") +
labs(caption = "2022-07-23 data") +
theme(axis.text = element_text(size = 12),
title = element_text(size = 12,face="bold"),
panel.border= element_rect(size=2,color="black",fill=NA)) ->
nn_gg

plot_gg(nn_gg, multicore = TRUE, width = 6, height = 5.5, scale = 300,
background = "#afceff",shadowcolor = "#3a4f70")

And I am sending a few rows of my sample data:

head(btcUSDT)
time price volume
1 1.658332e+12 24177.8 1.533
2 1.658332e+12 24178.3 1.535
3 1.658332e+12 24179.1 3.650
4 1.658332e+12 24179.8 3.950
5 1.658332e+12 24179.9 4.241
6 1.658332e+12 24180.0 35.546

class(btcUSDT$time)
[1] "numeric"
class(btcUSDT$price)
[1] "numeric"
class(btcUSDT$volume)
[1] "numeric"

And an image result that I get Plot results

Please note that samples from https://www.tylermw.com/3d-ggplots-with-rayshader/ are reproduced properly in my R Studio so all libraries works fine. The file with data that I am using is available here Data used

Can anyone help to solve the problem? Thanks

I have changed the data set and values of z axis are plotted but it seems that these have not the correct size (z values seems small). In the previous data set z ranges are [0.60] now are [0,300] and this is the new result that i get new 3d snapshot

The R script is pretty much the same:

library(tidyverse)
library(viridis)
library(rayshader)
library(ggplot2)

dec = "."   

binancebtcUSDT <- read.csv("test orderbook/data/btcusdtbinance 20220720.csv",header=TRUE,sep=",",dec=dec,strip.white = TRUE)
head(binancebtcUSDT)

class(binancebtcUSDT$time)
class(binancebtcUSDT$price)
class(binancebtcUSDT$volume)

max(binancebtcUSDT$price)
min(binancebtcUSDT$price)

max(binancebtcUSDT$volume)
min(binancebtcUSDT$volume)


binancebtcUSDT %>% 
  ggplot(aes(x = time, y = price)) + 
  geom_tile(aes(color = volume),size=3, alpha= 0.1) + 
  scale_color_gradient(low = "dark red", high = "steelblue", na.value = NA) +
  scale_x_continuous("Time", expand=c(0,0)) +
  scale_y_discrete("Price", expand=c(0,0)) +
  ggtitle("Binance BTC/USDT order book") +
  labs(caption = "2022-07-20 data") +
  coord_cartesian(ylim=c(23418.3, 24196.3), xlim=c(1658331502525,1658339456644)) +
  theme(axis.text = element_text(size = 12),
        title = element_text(size = 12,face="bold"),
        panel.border= element_rect(size=2,color="black",fill=NA)) -> 
  binance_plt

plot_gg(binance_plt, multicore = TRUE, width = 6, height = 5.5, scale = 300, 
        background = "#afceff",shadowcolor = "#3a4f70")

I have tried to change the scale in plot_gg but only the z scale outside the plot changes. This is the new dataset that I am using new data set Any idea to solve the new issue? Thanks

I have found out that the data set contains a few elements in the range 100-300 and this causes such representation issue. Is there a way to change the scale of the z values?


Solution

  • looks like "fill" is not the right argument for geom_tile: you need to use color:

    btcUSDT %>%
      ggplot(aes(x = time, y = price) ) +
      #geom_point(aes(color=volume))+
      geom_tile(aes(color = volume),size=3, alpha= 0.1)+
      scale_color_gradient(low = "dark red", high = "steelblue", na.value = NA)
    

    enter image description here