尝试绘制不同日期的加密货币的多个索引价格

mkr*_*ink 5 api r ggplot2 cryptocurrency

我正在尝试为几种货币创建一个很好的索引价格图表,以便我可以跟踪不同项目和价格水平的相对表现。

下面是我的虚拟代码。我已经尝试了很多事情,但这只是我所能得到的......

原代码的R图:HEX和BTC的价格

我希望在进行过程中添加其他货币。

最后,它只是一个包含多个列的数据框,所有列都需要从同一点开始,时间戳是无关紧要的,我可以只绘制系列或将它们全部移动以从同一位置开始。

这就是我想要实现的目标:

同源项目的指数价格


# Dummy data that recreates my problem - two frames with different starting dates and an indexed value of the closing price.

n1 <- 366

dat1 <- data.frame(timestamp=seq.Date(as.Date("2012-12-26"), as.Date("2013-12-26"), "day"),
                   index.btc=seq(from = 1, to = n1, by=1, replace=TRUE)
                   )
dat2 <- data.frame(timestamp=seq.Date(as.Date("2013-12-26"), as.Date("2014-12-26"), "day"),
                   index.hex=seq(from = 1, to = n1, by=1, replace=TRUE)
                  )
# Merging data
jointdataset2 <- merge(dat1, dat2, by = 'timestamp', all = TRUE)

# Creating plottable data with melt function
jointdataset_plot <- melt(jointdataset2 ,  id.vars = 'timestamp', variable.name = 'project')

# plot on same grid, each series colored differently -- 
# good if the series have same scale (they have but different starting date)
ggplot(jointdataset_plot, aes(timestamp,value)) + 
  geom_line(aes(colour = project)) +
  scale_y_log10()

# Can also plot like this
ggplot() + geom_line(data = dat1, aes(timestamp,index.btc),
                     color = "blue", 
                     size = 1) +
  geom_line(data = dat2, aes(timestamp,index.hex),
            color = "red", 
            size = 1) +
  labs(x = "Time", 
       y = "Indexed Price",
       title ="Indexed historical price (daily close index)",
       subtitle = "Candlesticks - data by nomics.com") +
  scale_x_date(date_labels = "%Y (%b)", date_breaks = "1 year", date_minor_breaks = "1 month") +
  scale_y_log10() +
  theme_bw()

Run Code Online (Sandbox Code Playgroud)

如果我删除时间戳,并从其中一个数据帧中删除 N/As,那么我是否能够在两个帧中创建一个 ID 列(从 1 开始,相同的计数器)并将它们合并到 ID 计数器 1 处,以便原点对齐?

小智 1



n1 <- 366

dat1 <- data.frame(timestamp=seq.Date(as.Date("2012-12-26"), as.Date("2013-12-26"), "day"),
                   index.btc=cumsum(sample(-2:10, n1, replace=TRUE))
)
dat2 <- data.frame(timestamp=seq.Date(as.Date("2013-12-26"), as.Date("2014-12-26"), "day"),
                   index.hex=cumsum(sample(-2:10, n1, replace=TRUE))
)

dat1$timestamp<- seq(length(dat1$timestamp))
dat2$timestamp<- seq(length(dat2$timestamp))

# Merging data
jointdataset2 <- merge(dat1, dat2, by = 'timestamp', all = TRUE)

# Creating plottable data with melt function
jointdataset_plot <- melt(jointdataset2 ,  id.vars = 'timestamp', variable.name = 'project')
# plot on same grid, each series colored differently -- 
# good if the series have same scale (they have but different starting date)
ggplot(jointdataset_plot, aes(timestamp,value)) + 
  geom_line(aes(colour = project)) +
  scale_y_log10()

# Can also plot like this
ggplot() + geom_line(data = dat1, aes(timestamp,index.btc),
                     color = "blue", 
                     size = 1) +
  geom_line(data = dat2, aes(timestamp,index.hex),
            color = "red", 
            size = 1) +
  labs(x = "Time", 
       y = "Indexed Price",
       title ="Indexed historical price (daily close index)",
       subtitle = "Candlesticks - data by nomics.com") +
  scale_x_continuous() +
  scale_y_log10() +
  theme_bw()


Run Code Online (Sandbox Code Playgroud)

在此输入图像描述