本文介绍了ggplot2:从x轴日期删除周末和假期的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到问题,在ggplot2中绘制股票数据,并带有包含周末和假期差距的x轴。



上面的情节(无红色框)由上述代码段生成。以下图表是尝试某些解决方案时的一些问题。首先,如果我尝试使用数据框架的索引,我将生成我漂亮的图形,但是x轴是不准确的;数据目前在十月份结束,但在下面的情况下,它将于七月份结束:

  xaxis<  -  factor(rownames(input),ordered = TRUE)

如果我使用包bdscale,但是网格线更干净,同样的问题会消除水平刻度:



  p   


解决方案

无法让OHLC工作 - 认为您需要一个自定义的 geom



我知道这不是你要求的,但是我可以尝试一下美味的蜡烛图吗?


$ b $
库(ggplot2)
库(quantmod)
库(magrittr)
库(bdscale)
库b $ b库(scale)

getSymbols(SPY,from = Sys.Date() - 1460,to = Sys.Date(),adjust = TRUE,auto.assign = TRUE)

input< - data.frame(SPY [2015 /])%>%
set_names(c(open,high,low ,volume,adjusted))%>%
mutate(date = as.Date(rownames(。)))

input%>%ggplot(aes = date,ymin = low,ymax = high,lower = pmin(open,close),upper = pmax(open,close),
fill = open< close,group = date,middle = pmin(open,close )+
geom_boxplot(stat ='identity')+
ggtitle(SPY:2015)+
xlab('')+ ylab('')+主题(图例) position ='none')+
scale_x_bd(business.dates = input $ date,max.major.breaks = 10,labels = date_format(%b %y))

running into issues while plotting stock data in ggplot2 and with an x-axis that contains gaps from weekends and holidays. this post has been very helpful, but i run into a variety of issues when trying to use ordered factors.

library(xts)
library(grid)
library(dplyr)
library(scales)
library(bdscale)
library(ggplot2)
library(quantmod)

getSymbols("SPY", from = Sys.Date() - 1460, to = Sys.Date(), adjust = TRUE, auto.assign = TRUE)

input <- data.frame(SPY["2015/"])
names(input) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")

# i've tried changing rownames() to index(), and the plot looks good, but the x-axis is inaccurate
# i've also tried as.factor()
xaxis <- as.Date(rownames(input))
input$xaxis <- xaxis

p <- ggplot(input)
p <- p + geom_segment(aes(x = xaxis, xend = xaxis, y = Low, yend = High), size = 0.50)           # body
p <- p + geom_segment(aes(x = xaxis - 0.4, xend = xaxis, y = Open, yend = Open), size = 0.90)    # open
p <- p + geom_segment(aes(x = xaxis, xend = xaxis + 0.4, y = Close, yend = Close), size = 0.90)  # close
p <- p + scale_y_continuous(scale_y_log10())
p + ggtitle("SPY: 2015")

The plot above (sans red boxes) is generated with the above code segment. And the following charts are some of the issues when attempting some solutions. First, if I try using the data frame's index, I will generate I nice looking graph, but the x-axis is inaccurate; the data currently ends in October, but in the plot below it ends in July:

xaxis <- as.Date(index(input))

Second, if I try coercing the rownames to an ordered factor, I lose my horizontal tick data (representing the open and the close).

xaxis <- factor(rownames(input), ordered = TRUE)

The same issue of removing the horizontal ticks happens if I use the package bdscale, but the gridlines are cleaner:

p <- p + scale_x_bd(business.dates = xaxis)
解决方案

Haven't been able to get the OHLC to work - think you'd need a custom geom.

I know it isn't exactly what you asked for, but may I tempt you with a delicious candle chart instead?

library(dplyr)
library(bdscale)
library(ggplot2)
library(quantmod)
library(magrittr)
library(scales)

getSymbols("SPY", from = Sys.Date() - 1460, to = Sys.Date(), adjust = TRUE, auto.assign = TRUE)

input <- data.frame(SPY["2015/"]) %>%
  set_names(c("open", "high", "low", "close", "volume", "adjusted")) %>%
  mutate(date=as.Date(rownames(.)))

input %>% ggplot(aes(x=date, ymin=low, ymax=high, lower=pmin(open,close), upper=pmax(open,close),
                     fill=open<close, group=date, middle=pmin(open,close))) +
  geom_boxplot(stat='identity') +
  ggtitle("SPY: 2015") +
  xlab('') + ylab('') + theme(legend.position='none') +
  scale_x_bd(business.dates=input$date, max.major.breaks=10, labels=date_format("%b '%y"))

这篇关于ggplot2:从x轴日期删除周末和假期的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:25