簡體   English   中英

在一個圖表中繪制兩個折線圖,共享時間 x 軸位於 R 軸。日期不起作用

[英]Plot two line graphs within one chart with shared time x-axis in R axis.Date not working

在 R 中運行以下代碼:

#################### PLOT DIAMOND HANDS AND RETURNS ##################### 

## add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 6) + 0.1)

## Plot first set of data and draw its axis
plot(df$time, df$returns, pch=16, axes=FALSE, ylim=c(0,-0.4), xlab="", ylab="", 
   type="l",col="black", main="GME Returns and Diamond Hands popularity")
axis(2, ylim=c(0,1),col="black",las=1)  ## las=1 makes horizontal labels

mtext("GME Returns",side=2,line=2.5)
box()

## Allow a second plot on the same graph
par(new=TRUE)

## Plot the second plot and put axis scale on right
plot(df$time, df$Diamond.Hands, pch=15,  xlab="", ylab="", ylim=c(0,1), 
    axes=FALSE, type="l", col="red")
## a little farther out (line=4) to make room for labels
mtext("Diamond Hands",side=4,col="red",line=4) 
axis(4, ylim=c(0,1), col="red",col.axis="red",las=1)

## Draw the time axis
axis(1,pretty(range(df$time),10))


# axis(1,pretty(range(df$time),10))
mtext("Time",side=1,col="black",line=2.5)  

## Add Legend
# legend("topleft",legend=c("GME Returns","Diamond Hands Popularity"),
# text.col=c("black","red"),pch=c(16,15),col=c("black","red"))

結果如下所示: 由於時間格式不正確。 時間數據本身之前定義為:

df$time <- strptime(df$time, format = "%Y-%m-%d %H:%M", tz = 'GMT') 

誰能幫我正確格式化它?

隨着axis.Date軸正在消失在此處輸入圖像描述

也許您也知道如何消除時間序列中的空白?

非常感謝!

編輯:數據

時間 返回 鑽石手
2021-02-16 10:00:00 -0.0037586920 0.4583333
2021-02-16 11:00:00 -0.0157776108 0.5000000
2021-02-16 21:00:00 -0.50761421 0.43948956
2021-02-17 08:00:00 -0.00142141 0.89114565
2021-02-17 09:00:00 -0.48694561 0.15894415
2021-02-17 10:00:00 -0.45861415 0.35784893
2021-02-17 11:00:00 -0.56869411 0.32154861
2021-02-17 12:00:00 -0.21356147 0.48692132
2021-02-17 18:00:00 -0.21345648 0.12345671
2021-02-17 19:00:00 -0.56521356 0.23148489
2021-02-17 20:00:00 -0.75656187 0.35258644

通過ggplot2解決了它:

#Filter only for negative return hours
df1 <- df %>% filter(returns < 0)

# Calculate moving average of returns for 10 days
df1$MAreturns <- TTR::SMA(df1$returns,n=10)

# Calculate moving average of Diamond Hands for 10 days
df1$Diamond.Hands=na.approx(df1$Diamond.Hands)
df1$MADiamond.Hands <- TTR::SMA(df1$Diamond.Hands,n=10)
df1$MADiamond.Hands <- df1$MADiamond.Hands*-1
head(df1)

# Plot graph
ggplot(data = df1, aes(x = as.POSIXct(time), y = returns)) +
  geom_line(alpha = 1/5)+
  # coord_cartesian(ylim=c(0,-0.2)) +
  geom_line(aes(y=Diamond.Hands*-1), alpha = 1/5)+
  geom_line(aes(y=MAreturns, color="10 day moving average of GME returns"))+
  geom_line(aes(y=MADiamond.Hands, color="10 day moving average of Diamond Hands"))+
  xlab("Time")+
  ylab("Negative GME returns")+
  scale_y_continuous(trans = "reverse", limits=c(0,-0.5), sec.axis = sec_axis(trans = ~.*-1, name="Popularity of diamond hands flair"))+
  labs(color='Legend')+
  scale_color_manual(name = "Legend", 
        values = c("10 day moving average of Diamond Hands" = "blue", 
                "10 day moving average of GME returns" = "red"))+
  theme_bw()+
  theme(legend.position = c(0.125, 0.925))+
  theme(text = element_text(size = 14))+
  theme(plot.margin=unit(c(1,3,1,1), "cm"))+
  theme(axis.title.y.right = element_text(vjust=3))+
  theme(axis.title.y.left = element_text(vjust=3))+
  scale_x_datetime(
    breaks = seq(as.POSIXct("2021-02-16 00:59:00 CET"),
                 as.POSIXct("2021-10-01 00:59:00 CET"), "1 month"),
    labels = date_format("%b", tz = "CET"),
    expand = c(0, 0),
    limits = c(
      as.POSIXct("2021-02-16 00:00:00 CET"),
      as.POSIXct("2021-10-01 00:00:00 CET")
    )
  )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM