Hi Ogbos, While this solution is not entirely correct, I think it is a start. First I took your data files and made them "sourceable" by adding "FD[1|2]<-" at the top and renaming them "FD1.R" and "FD2.R". Running the following code produces something that is at least close to what you want. The code is fairly well commented so it should be easy to see what I have done:
# read in the FD1 data source("FD1.R") # read in the FD2 data source("FD2.R") # convert year-month-day columns to dates FD1$data.year<-FD1$data.year+ifelse(FD1$data.year < 50,2000,1900) FD1$date<-as.Date(paste(FD1$data.year,FD1$data.month,FD1$data.day,sep="-"), format="%Y-%m-%d") FD2$data.year<-FD2$data.year+ifelse(FD2$data.year < 50,2000,1900) FD2$date<-as.Date(paste(FD2$data.year,FD2$data.month,FD2$data.day,sep="-"), format="%Y-%m-%d") # check the ranges for overlap range(FD1$date) range(FD2$date) # get the overall range of the plot xlim<-range(c(FD1$date,FD2$date)) # FD1 spans the date range so xlim is not really needed here # now get the counts for each data set FD1counts<-as.vector(table(cut(FD1$date,breaks="years"))) # FD2 is missing 1996, 1997 and 2016 so add zeros at the beginning and end FD2counts<-c(0,0,as.vector(table(cut(FD2$date,breaks="years"))),0) # set up the bar colors barcol<-matrix(c(rep("red",2),rep("blue",18),"red", rep("red",2),rep("green",18),"red"),nrow=2,byrow=TRUE) # use barp as barplot can't do the colors library(plotrix) barp(rbind(FD1counts,FD2counts),names.arg=1996:2016, main="Observation counts for FD1 and FD2", xlab="Year",ylab="Observations",col=barcol) legend(12,80,c("FD1 only","FD1 & FD2","FD2 & FD1"), fill=c("red","blue","green")) This shows the overlap in blue and green. You can make the overlap colors whatever you like. It doesn't account for the fact that FD2 only overlaps for part of a year on both ends. You may not be worried about this. Jim On Fri, May 8, 2020 at 4:07 PM Ogbos Okike <giftedlife2...@gmail.com> wrote: > > Dear Jim, > Thank you for looking into this. > Sorry, there was actually no overlap in the small part of the data I > reported. My error of omission. > > So when I run my full data with the adjustment you made, I got some thing > that was far from what I was expecting. That tell me that I need to send the > complete data to enable you correctly adjust the code, especially in the > light of the missing/present overlap. > I have used deput function to attach the two files. Please use any symbol to > depict the color/mark/legend of the overlap dates (just to enable the reader > visualize what is going on). I am actually trying to display event > frequency/occurrence per year. > > Thank you and warmest regards > Ogbos > > On Fri, May 8, 2020 at 1:13 AM Jim Lemon <drjimle...@gmail.com> wrote: >> >> Hi Ogbos, >> I don't think that your example allows us to work out what you are >> trying to do. For one thing, "x1" and "x2" do not overlap. Also, you >> are plotting the frequencies of dates of observations, which may not >> be what you want. >> The following code will correctly display your example: >> >> hist(x1,breaks="years",freq=T,axes=F,xlim=c(9400,11100),col=c1a) >> hist(x2,breaks="years",freq=T,axes=F,add=T,col=c2a) >> axis.Date(1, at=seq(as.Date(min(x1)), as.Date(max(x2)), by="years")) >> axis(2) >> legend("topleft", c("AUTO", "MANUAL"), fill=c("red", "blue")) >> >> What it is displaying is the frequency of observations in your two >> vectors by calendar year. If this is what you want, and you can >> explain how you would like "overlap" to be displayed, we can probably >> provide better help. >> >> Jim >> >> >> On Fri, May 8, 2020 at 7:01 AM Ogbos Okike <giftedlife2...@gmail.com> wrote: >> > >> > Dear Experts, >> > Greetings. >> > >> > I am trying to display two datasets in a histogram. I have been able to >> > plot the graph and added the legend for the two colors. I am, however, >> > having difficulties adding a legend to represent the regions of overlap >> > (the third legend). Below are my data and code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.