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

Trying to plot multiple indexed prices of cryptocurrencies with different dates

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

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

R plot of the orignal code: prices of HEX and BTC

我希望添加其他货币。

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

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

Indexed prices of projects starting at same origin


# 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()

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

你的示例数据重叠,所以我更改了dat2:

library(dplyr);library(tidyr)
n1 <- 366
n2 <- 500
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 = n2, length.out=n1))

full_join(dat1,dat2) %>%
  pivot_longer(-timestamp, names_to = "index", values_to = "price") %>%
  filter(!is.na(price)) %>%
  group_by(index) %>%
  mutate(timestamp = as.integer(timestamp - min(timestamp))) -> plotdata

ggplot(plotdata, aes(x = as.integer(timestamp),
                     y = price, color = index)) +
  geom_line() +
  labs(x = "Time (Days)", 
       y = "Indexed Price",
       title ="Indexed historical price (daily close index)",
       subtitle = "Candlesticks - data by nomics.com") +
  scale_y_log10() +
  theme_bw()



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