2012-11-11 10 views
6

Je voudrais faire un histogramme vertical. Idéalement, je devrais être capable de mettre plusieurs sur une seule parcelle par jour.Histogramme vertical

Si cela peut être combiné avec quantmod experimental chart_Series ou une autre bibliothèque capable de dessiner des barres pour une série temporelle qui serait géniale. Veuillez vous référer à la capture d'écran ci-jointe. Idéalement, je pourrais tracer quelque chose comme ça.

Y a-t-il des bibliothèques intégrées ou existantes qui peuvent vous aider?

Market Profile Example

Répondre

1

Si vous utilisez des graphiques de la grille, vous pouvez créer des fenêtres pivotés whereever vous les voulez et l'intrigue à la fenêtre pivotée. Vous avez juste besoin d'une fonction qui tracera en utilisant des graphiques de grille dans une fenêtre spécifiée, je suggérerais ggplot2 ou éventuellement treillis pour cela. Dans les graphiques de base, vous pouvez écrire votre propre fonction pour tracer l'histogramme pivoté (modifiez la fonction plot.histogram ou écrivez la vôtre à partir de zéro en utilisant rect ou d'autres outils). Ensuite, vous pouvez utiliser la fonction subplot du module TeachingDemos pour placer l'intrigue où vous voulez sur un plus grand terrain.

3

Les graphiques de violon peuvent être assez proches de ce que vous voulez. Ce sont des parcelles de densité qui ont été réfléchies à travers un axe, comme un hybride d'un boxplot et d'un plot de densité. (Beaucoup plus facile à comprendre par exemple que la description :-).)

Voici un simple (un peu laid) exemple de la mise en œuvre de ggplot2 d'entre eux:

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

Un plus joli exemple serait être:

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

Pour inclure des plages au lieu de ou en plus de la ligne, vous devez utiliser geom_linerange ou geom_pointrange.

9

J'ai écrit quelque chose il y a environ un an pour faire des histogrammes verticaux dans les graphiques de base. Voilà, avec un exemple d'utilisation.

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) 
    x.l <- rep(x, n) 
    x.r <- x.l + hist$density * xscale 
    y.b <- hist$breaks[1:n] 
    y.t <- hist$breaks[2:(n + 1)] 

    rect(xleft = x.l, ybottom = y.b, xright = x.r, ytop = y.t, 
     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