Crubal Chenxi Li Crubal Chenxi Li - 11 months ago 91
R Question

Overlapping Histograms in R

I am overlapping two histograms, and would like to maintain each of them that are not in common, as well as the common area. (3 colors). I have the following codes:

hotel_auto<-subset(hotel_pr_curr,hotel_pr_curr$BFR_CONDITION_1 =="AUTO_BFR")
hotel_nonauto<-subset(hotel_pr_curr,hotel_pr_curr$BFR_CONDITION_1 == "NON_AUTO_BFR")

hist_curr1<- hist(hotel_auto$CURR_DIF_RTO, breaks = 20)
hist_curr2<- hist(hotel_nonauto$CURR_DIF_RTO, breaks = 20)

plot(hist_curr1,freq=FALSE,xlim=c(0,0.8),ylim=c(0,50),ylab="Percentage",col=rgb(0,0,1,1), main="BFR Current-Historical",xlab="Current Historical Difference")

plot(hist_curr2,freq=FALSE,xlim=c(0,0.8),ylim=c(0,50),ylab="Percentage",col=rgb(1,0.9,0,1), main="",xlab="", add = TRUE)
legend("topright", c("AUTO_BFR", "NONAUTO_BFR"), cex=0.75, fill=c("blue", "yellow"))

enter image description here

And it just has two colors, can I have three colors of them as I just mentioned?


The last alpha value in rgb is for alpha, so if you change your colours to col=rgb(0,0,1,0.5) and col=rgb(1,0.9,0,0.5) you should be able to see the overlap.