I want to produce a bar plot with multiple ranges of data points highlighted differently. The following question here at r - ggplot2 - highlighting selected points and strange behavior is close to what I want to do except that instead of subsetting and highlighting only one range of datapoints, I want to subset multiple ranges of data points and generate a differentially colored bar plot.
I have used this code (adapted from that question above) to red-highlight a subset of data from 10 to 30.
a <- 1:50
b <- rnorm(50)
mydata <- data.frame(a=a,b=b)
mydata[10:30,]
ggplot(mydata,aes(x=a,y=b)) + geom_point(colour="blue") + geom_point(data=mydata[10:30,], aes(x=a, y=b), colour="red")
How can I modify the above code to differentially highlight multiple subsets, say, 1:9 (green), 10:30 (red) and 40:50 (cyan)? Another issue is that if I modify that code by changing geom_point
to geom_bar
in ggplot to produce a bar plot the code returns an error, Error: stat_count() must not be used with a y aesthetic.
Thanks in advance.