Vertical Histogram

后端 未结 3 1404
抹茶落季
抹茶落季 2020-12-17 01:14

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_S

3条回答
  •  生来不讨喜
    2020-12-17 01:32

    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.

提交回复
热议问题