Alex Rivera | Logout

Vertical Histogram

Asked 2012-11-11T00:32:42.830
8

I'd like to do a vertical histogram. Ideally I should be able to put multiple on a single plot per day.

If this could be combined with quantmod experimental chart_Series or some other library capable of drawing bars for a time series that would be great. Please see the attached screenshot. Ideally I could plot something like this.

Is there anything built in or existing libraries that can help with this?

Market Profile Example

Edit
Report

1 Answer

4

Violin plots might be close enough to what you want. They are density plots that have been mirrored through one axis, like a hybrid of a boxplot and a density plot. (Much easier to understanding by example than description. :-) )

Here is a simple (somewhat ugly) example of the ggplot2 implementation of them:

library(ggplot2)
library(lubridate)

data(economics) #sample dataset

# calculate year to group by using lubridate's year function
economics$year<-year(economics$date)

# get a subset 
subset<-economics[economics$year>2003&economics$year<2007,]    

ggplot(subset,aes(x=date,y=unemploy))+
    geom_line()+geom_violin(aes(group=year),alpha=0.5)

violin plot over a line plot of a time series

A prettier example would be:

ggplot(subset,aes(x=date,y=unemploy))+ 
    geom_violin(aes(group=year,colour=year,fill=year),alpha=0.5, 
    kernel="rectangular")+    # passes to stat_density, makes violin rectangular 
    geom_line(size=1.5)+      # make the line (wider than normal)
    xlab("Year")+             # label one axis
    ylab("Unemployment")+     # label the other
    theme_bw()+                     # make white background on plot
    theme(legend.position = "none") # suppress legend

enter image description here

To include ranges instead of or in addition to the line, you would use geom_linerange or geom_pointrange.

answered 2012-11-11T16:20:26.733

Your Answer