在R中将图表制作成类似于Bloomberg的图表

3

4
你到目前为止尝试了什么? - user3710546
quantmod包中的chartSeries(...)函数就是为此而设计的。它非常接近你所需要的。但如果你想要在价格时间序列下方进行渐变填充,那么我认为这是不可能的。 - jlhoward
没问题。在R控制台中输入?theme。考虑将你的工作添加到ggthemes - hrbrmstr
1个回答

6
这是初步的版本,它没有渐变填充,收盘价填充是一个矩形而不是指向图表的箭头(我尝试使用annotategeom="segment"来获得箭头背景,但看起来这在日期方面不能正常工作)。为了一般使用,还需要一些逻辑来决定右侧边缘的收盘价文本所需的区域大小,而不是硬编码。我还没有包括一个带高、低、平均等的面板,这可以通过annotate添加。
library(ggplot2)

set.seed(199)
dat = data.frame(date = seq(as.Date("2013/10/01"), as.Date("2013/12/31"), by="1 day"),
                 price = cumsum(rnorm(92, 0, 1)) + 100)

ggplot(dat, aes(date, y=price)) +
  geom_area(fill="navyblue", colour="white", alpha=0.5) +
  theme(plot.background=element_rect(fill="black"),
        panel.background=element_rect(fill="#101040"),
        panel.grid.minor=element_blank(),
        panel.grid.major=element_line(linetype=2),
        axis.text=element_text(size=15, colour="white")) +
  coord_cartesian(ylim=c(min(dat$price) - 1, max(dat$price) + 1),
                  xlim=c(min(dat$date)-2, max(dat$date)+10)) +
  annotate("rect", xmin=max(dat$date) + 0.75, xmax=max(dat$date) + 7.25, 
           ymin=dat$price[dat$date==max(dat$date)] - 0.25, 
           ymax=dat$price[dat$date==max(dat$date)] + 0.25, fill="white", colour="black") +
  annotate("text", max(dat$date) + 1, dat$price[dat$date==max(dat$date)], 
           label=paste0("$", round(dat$price[dat$date==max(dat$date)],2)), 
           colour="black", hjust=0)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接