Crubal Chenxi Li - 1 year ago 137
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)

hist_curr1\$density<-hist_curr1\$counts/sum(hist_curr1\$counts)*100
hist_curr2\$density<-hist_curr2\$counts/sum(hist_curr2\$counts)*100
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")

legend("topright", c("AUTO_BFR", "NONAUTO_BFR"), cex=0.75, fill=c("blue", "yellow"))
``````

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

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.