Vertical bar graph

I would like to make a vertical bar chart. Ideally, I should be able to put several on one plot per day.

If this can be combined with an experimental quantmod chart_Series or another library that can draw bars for a time series, that would be great. See the attached screenshot. Ideally, I could do something like this.

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

Market Profile Example

+6
r quantmod
source share
3 answers

I wrote something a year ago to make vertical histograms in the base chart. Here it is, with an example of use.

VerticalHist <- function(x, xscale = NULL, xwidth, hist, fillCol = "gray80", lineCol = "gray40") { ## x (required) is the x position to draw the histogram ## xscale (optional) is the "height" of the tallest bar (horizontally), ## it has sensible default behavior ## xwidth (required) is the horizontal spacing between histograms ## hist (required) is an object of type "histogram" ## (or a list / df with $breaks and $density) ## fillCol and lineCol... exactly what you think. binWidth <- hist$breaks[2] - hist$breaks[1] if (is.null(xscale)) xscale <- xwidth * 0.90 / max(hist$density) n <- length(hist$density) xl <- rep(x, n) xr <- xl + hist$density * xscale yb <- hist$breaks[1:n] yt <- hist$breaks[2:(n + 1)] rect(xleft = xl, ybottom = yb, xright = xr, ytop = yt, col = fillCol, border = lineCol) } ## Usage example require(plyr) ## Just needed for the round_any() in this example n <- 1000 numberOfHists <- 4 data <- data.frame(ReleaseDOY = rnorm(n, 110, 20), bin = as.factor(rep(c(1, 2, 3, 4), n / 4))) binWidth <- 1 binStarts <- c(1, 2, 3, 4) binMids <- binStarts + binWidth / 2 axisCol <- "gray80" ## Data handling DOYrange <- range(data$ReleaseDOY) DOYrange <- c(round_any(DOYrange[1], 15, floor), round_any(DOYrange[2], 15, ceiling)) ## Get the histogram obects histList <- with(data, tapply(ReleaseDOY, bin, hist, plot = FALSE, breaks = seq(DOYrange[1], DOYrange[2], by = 5))) DOYmean <- with(data, tapply(ReleaseDOY, bin, mean)) ## Plotting par(mar = c(5, 5, 1, 1) + .1) plot(c(0, 5), DOYrange, type = "n", ann = FALSE, axes = FALSE, xaxs = "i", yaxs = "i") axis(1, cex.axis = 1.2, col = axisCol) mtext(side = 1, outer = F, line = 3, "Length at tagging (mm)", cex = 1.2) axis(2, cex.axis = 1.2, las = 1, line = -.7, col = "white", at = c(75, 107, 138, 169), labels = c("March", "April", "May", "June"), tck = 0) mtext(side = 2, outer = F, line = 3.5, "Date tagged", cex = 1.2) box(bty = "L", col = axisCol) ## Gridlines abline(h = c(60, 92, 123, 154, 184), col = "gray80") biggestDensity <- max(unlist(lapply(histList, function(h){max(h[[4]])}))) xscale <- binWidth * .9 / biggestDensity ## Plot the histograms for (lengthBin in 1:numberOfHists) { VerticalHist(binStarts[lengthBin], xscale = xscale, xwidth = binWidth, histList[[lengthBin]]) } 

verticalhistograms

+10
source share

Scripting plots may be close enough to what you want. They are density charts that are reflected across a single axis, such as a hybrid of a rectangle and a density graph. (It is much easier to understand an example than a description. :-))

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

 library(ggplot2) library(lubridate) data(economics) #sample dataset # calculate year to group by using lubridate 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

An 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 a line, you must use geom_linerange or geom_pointrange.

+3
source share

If you use grid graphics, you can create rotary viewports, regardless of whether you want them and draw them on the rotated viewport. You just need a function that will display the grid graphics in the specified viewport, I would suggest ggplot2 or, possibly, a grid for this.

In the base graph, you can write your own function for constructing a rotated histogram (change the plot.histogram function or just write your own from scratch using rect or other tools). You can then use the subplot function from the TeachingDemos package to place the plot anywhere you want on a larger plot.

+1
source share

All Articles