Search code examples
rggplot2data-visualizationbloomberg

Make Plot look like a Bloomberg Plot in R


I am trying to make a plot look like the ones of a Bloomberg terminal in R, and I found the following post for Mathematica:

https://mathematica.stackexchange.com/questions/48185/make-plot-look-like-bloomberg-terminal

My question is, can this be done in R I am thinking with ggplot2?


Solution

  • Here's a first cut. It doesn't have gradient fill, and the closing price fill is a rectangle rather than a pointer to the graph (I tried annotate with geom="segment" to get an arrow background, but it looks like this doesn't work properly with dates). For general use it also needs some logic, rather than hard-coding, for deciding how much area to allocate for the closing price text at the right edge. I also haven't included a panel with high, low, average, etc., which can be added with 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