I have a number of data files that Id like to explore in R, each of which I plan to use to create a data frame with a column variable, which for this question is marked with the Ill icon as foo . The range foo lies along the interval [0, 7000]. As part of my data exploration research, Id wanted to create a 1D histogram foo , but with a slight twist: foo values ββin the range between (1000, 7000) are especially interesting to me, and therefore Id would like to color individual sheets of the histogram in this data range using the color picker (i.e. because later in the end I intend to reuse the same palette to display data from some other columns that I temporarily skipped from the data frame so as not to leave my question unnecessarily complicated). Conversely, the values ββof foo in the range [0,1000] are not so interesting to me, however Id still likes to see them on the histogram, although it is colored gray, in cases where any values ββare present.
In my code example below, I created an artificial data sample and tried to plot the histogram with ggplot2 , choosing fill colors with scale_fill_manual() . I got a multi-color histogram, however it does not look as expected: ggplot2 seems to have ignored my instructions on where to place the gaps between the colors. In particular, the problem seems to be related to missing data: intervals that have no data do not seem to display in color, although that was my intention, which they should be. It also means that the gray color ends up being displayed on the interval (1000, 1500) instead of [0, 1000], as I expected.
My question is: how can I get ggplot2 to assign certain color fill codes for certain data ranges, even if some intervals are empty and have no data, and histograms corresponding to these intervals are therefore not generated?
Ive included the original version of my code below, along with a dummy example data framework plus a manual annotated version of the output it produces.
library(ggplot2)

r ggplot2
stachyra
source share