Bar color change ggplot based on increase/decrease of data r -
i need change color of bars in ggplot bar graph based on if data increasing or decreasing.
i have looked this question helped little, colors continuing default ggplot's colors, bar graphs come out weird when compared wanted , legend says are.
b<- data.frame(day=c('05/22','05/23','05/24','05/25','05/26','05/27','05/28','05/29','05/30','05/31','06/01','06/02','06/03','06/04','06/05','06/06','06/07','06/08','06/09','06/10','06/11','06/12','06/13','06/14','06/15','06/16','06/17','06/18','06/19','06/20','06/21','06/22','06/23','06/24','06/25'),temp.diff=c(10.1,8.7,11.4,11.4,11.6,10.7,9.6,11.0,10.0,10.7,9.5,10.3,8.4,9.0,10.3,11.3,12.7,14.5,12.5,13.2,16.5,19.1,14.6,14.0,15.3,13.0,10.1,8.4,4.6,4.3,4.7,2.7,1.6,1.8,1.9)) delta<- (sign(diff(b$temp.diff)) == 1 ) + 0 delta<-as.data.table(delta) delta<-rbind(delta,data.frame(delta=0)) b$delta<-delta l <- max(b$temp.diff) q<- b[!is.na(b$delta) & b$delta == 1, 'color'] <- 'green' w<- b[!is.na(b$delta) & b$delta == 0, 'color'] <- 'red' r <- b[!is.na(b$temp.diff) & (b$temp.diff) == l, 'color'] <-'purple' gg <- ggplot(b, aes(x=day, y=temp.diff, fill=color)) + geom_bar(stat='identity', position='identity') gg1 <- gg + annotate ('line', ymin=-5) gg2 <- gg + labs( x='date', y='mean temp diff') gg
the graph looks normal distribution curve. legend titles on graph don't match bar colors. don't understand why it doing this. code posted above step towards goal, not meeting fully. 'delta' used determine if temperature following 1 being analyzed of higher or lower temperature. if lower, 'delta' gives binary '0', while if higher, 'delta' gives binary '1'. found code here. in other words, if in delta column, rows '0,1,0', don't want '1' change colors because followed '0', meaning overall data still decreasing, , vice versa '1'.
i need code (i.e. delta rows '0' , '1') , bar colors on graph change if the next two temperatures higher ('0,1,1') or lower ('1,0,0') 1 analyzing, in order keep random fluctuations of temperature giving me different colors when not needed. on graph, when increasing in temperature, there random colored bar because preceding bar technically lower 1 being analyzed because @ moment, comparing single temperature following it. happens decreasing side of graph well--there random colored bar because preceding temperature higher 1 being analyzed.
i new r, , can't figure out need next in order graphs come out clean. if there way override '0,1,0', or change '1' '0' in order colors remain same. in advance.
is trying do:
library(dplyr) # add column marking decreases/increase/no change previous day b$diff = c(0, sign(diff(b$temp.diff))) # add column marking length of decrease/increase run b$rungroups = rep(1:length(rle(b$diff)[[1]]), rle(b$diff)[[1]]) # add column length of current run each run b = b %>% group_by(rungroups) %>% mutate(runlength=1:n()) # add group b$group=0 b$group[b$runlength >1 & b$diff == -1] = -1 b$group[b$runlength >1 & b$diff == 1] = 1 # highlight runs of 2 or more day-over-day decreases/increases gg <- ggplot(b, aes(x=day, y=temp.diff, fill=factor(group))) + geom_bar(stat='identity', position='identity') + scale_fill_manual(values=c("red","grey70","blue"), labels=c(">= 2-day run of decreases","no runs", ">= 2-day run of increases"), name="") + #annotate ('line', ymin=-5) + labs( x='date', y='mean temp diff') + theme_bw() + theme(axis.text.x=element_text(angle=-90, vjust=0.5))
if want color bars based on whether there increase or decrease previous day, can work directly original b
data frame, without modifications:
# plot day-over-day increase/decrease gg1 <- ggplot(b, aes(x=day, y=temp.diff, fill=factor(c(0,sign(diff(temp.diff)))))) + geom_bar(stat='identity', position='identity') + scale_fill_manual(values=c("red","grey70","blue"), labels=c("decrease","no change","increase"), name="") + #annotate ('line', ymin=-5) + labs( x='date', y='mean temp diff') + theme_bw() + theme(axis.text.x=element_text(angle=-90, vjust=0.5))
update 1: added loop remove single trend reversal surrounded @ least two-days of opposite trend , reset opposite trend.
# mark trend up, down, or same b$sign.diff = c(0,sign(diff(b$temp.diff))) # reverse trend sign in case of single-day reversals of +/- 2-day runs of opposite trend (i in 3:(nrow(b)-2)) { if (all(b[c(i-2,i-1,i+1,i+2), "sign.diff"] == -b[i, "sign.diff"])) { b[i,"sign.diff"] = -b[i, "sign.diff"] } } # plot day-over-day increase/decrease gg2 <- ggplot(b, aes(x=day, y=temp.diff, fill=factor(sign.diff))) + geom_bar(stat='identity', position='identity') + scale_fill_manual(values=c("red","grey70","blue"), labels=c("decrease","no change","increase"), name="") + #annotate ('line', ymin=-5) + labs( x='date', y='mean temp diff') + theme_bw() + theme(axis.text.x=element_text(angle=-90, vjust=0.5))
Comments
Post a Comment