r - Adding Bars in background of Bargraph -
i have been having issues adding recession bars in background of graph. proper way it? have tried define geom_rect(.....) background did not work out properly.
here code how data:
library(quantmod) library(dplyr) library(ggplot2) getsymbols("uspriv",src="fred") getsymbols("usgovt",src="fred") #convert data quantmod dataframes uspriv.df <- data.frame(date= index(uspriv),uspriv$uspriv) usgovt.df <- data.frame(date= index(usgovt),usgovt$usgovt) #calculate month-to-month difference d.uspriv <- dplyr::mutate(uspriv.df[-1,], duspriv = uspriv.df$uspriv[-1]-uspriv.df$uspriv[-nrow(uspriv.df)]) d.usgovt <- dplyr::mutate(usgovt.df[-1,], dusgovt = usgovt.df$usgovt[-1]-usgovt.df$usgovt[-nrow(usgovt.df)]) df <- dplyr::left_join(d.uspriv, d.usgovt, = "date") #shorten dataframe, starting in 2007 df.2007 <- dplyr::filter(df, date >= "2007-01-01") df1.2007 <- dplyr::select(df.2007, date, duspriv, dusgovt) df1 <- melt(df1.2007, id="date") ggplot(df1, aes(x=date, y=value)) + geom_bar(aes(fill=variable), stat="identity", position=position_dodge()) + scale_fill_brewer(palette="dark2")
this generates graph here:
what efficient way include recession bars? here code obtain data
getsymbols("usrec",src="fred") usrec.df <- data.frame(date= index(usrec), usrec$usrec)
if include recession data dataframe , melt it, how define ggplot part?
thank much.
here's more robust solution. should not fail when there multiple recessions (the irony of sentence...). user bergant
. note: still need ensure there matching usrec data time frame you're considering. i've done example starting in 1965.
library(quantmod) library(dplyr) library(reshape2) library(ggplot2) getsymbols("uspriv",src="fred") getsymbols("usgovt",src="fred") #convert data quantmod dataframes uspriv.df = data.frame(date= index(uspriv),uspriv$uspriv) usgovt.df = data.frame(date= index(usgovt),usgovt$usgovt) #calculate month-to-month difference d.uspriv = dplyr::mutate(uspriv.df[-1,], duspriv = uspriv.df$uspriv[-1]-uspriv.df$uspriv[-nrow(uspriv.df)]) d.usgovt = dplyr::mutate(usgovt.df[-1,], dusgovt = usgovt.df$usgovt[-1]-usgovt.df$usgovt[-nrow(usgovt.df)]) df = dplyr::left_join(d.uspriv, d.usgovt, = "date") #shorten dataframe, starting in 1965 df.1965 = dplyr::filter(df, date >= "1965-01-01") df1.1965 = dplyr::select(df.1965, date, duspriv, dusgovt) df1 = melt(df1.1965, id="date") ######### getsymbols("usrec",src="fred") usrec = data.frame(date= index(usrec), usrec$usrec) rownames(usrec) = null new_df = merge(df1, usrec) ############## # go out @bergant bit: dif = diff(new_df$usrec) new_df$status = factor(c(0, dif) - 2 * c(dif, 0), levels = -3:3) levels(new_df$status) = c(rep(0, 4), "start", "end", "start&end") ############## start_end_dt = data.frame( xmin = as.date(new_df$date[new_df$status == "start"]) , xmax = as.date(new_df$date[new_df$status == "end"]) ) ##############
and rest easy:
ggplot(new_df, aes(x=date, y=value)) + geom_rect( inherit.aes = false , data = start_end_dt , aes(xmin = xmin , xmax = xmax , ymin=-inf , ymax=+inf) , fill='gray' , alpha=0.5) + geom_bar(aes(fill=variable), stat="identity", position=position_dodge()) + scale_fill_brewer(palette="dark2") + theme_bw() + theme( panel.grid.minor = element_blank(), panel.grid.major = element_blank() )
Comments
Post a Comment