This is probably a very simple question, but I can't get my head around it. I'm producing a histogram as proposed in How to fill histogram with color gradient? and would like to change the colour scheme using scale_color_gradient2. The histogram bars should be coloured according to the continuous variable in my dataset, with "darkgreen" for the smallest values, "red" for the largest and "orange" for the ones in between. I also use the neat "trans" attribute of scale_color_gradient2 to log-transform my data within the plot. Unfortunately, applying scale_color_gradient2 does not work, as I'm already specifying the colour fill of the bars within the aes attributes of geom_histogram. Now, I'm pretty sure that ggplot2 offers such a functionality, but I can't figure out how. Do I need to prepare my dataset "manually" before applying scale_color_gradient2, i. e. do I have to use cut() to produce breaks and then assign the continuous data to those breaks?
Example code:
myData <- c(1.14697513, 1.17511522, 4.40715049, 12.46033118, 13.31236437,
13.80646742, 14.32024812, 14.27723102, 15.38885253, 18.5314918,
19.54137644, 19.84750311, 19.95955739, 22.16140883, 23.45692625,
26.39371955, 29.84970725, 19.71228586, 23.81598965, 28.8954292,
26.42070425, 24.70807915, 25.25013288, 29.06533739, 19.82372419,
27.8835174, 20.67534068, 21.48039058, 22.90360096, 23.79028599,
23.82270201, 24.75539892, 25.72394072, 24.3639409, 19.84061517,
22.13606982, 24.74780434, 22.37468515, 16.02038647, 15.03168204,
15.49245895, 15.33408853, 15.72013928, 15.92662432, 16.49098242,
31.21540968, 85.36487512, 24.89061536, 22.88268752, 25.45473677,
29.40085037, 24.09764601, 25.90179717, 29.04696969, 29.12589867,
37.06852817, 28.3928621, 27.97867109, 26.11118457, 25.62078501,
25.55879171, 25.47652264, 31.64679767, 28.84595255, 28.06412499,
23.15553981, 26.53143762, 23.88348507, 24.14657713, 25.01563365,
21.65575207, 15.54498242, 16.57195781, 16.79712558, 16.5264702,
15.59375338, 13.81176329, 14.7108595, 18.2395959, 23.32177567,
45.4972562, 63.74298896, 56.24248751, 56.28860198, 48.77159908,
51.359598, 28.59380079, 18.70095675, 15.86502211, 4.99145761,
1.20619987, 1.28706938, 1.39360758, 1.33117437, 1.28165222, 1.20450226,
1.18710708, 1.27730078, 1.55985527, 1.92654237, 1.89138402, 1.29368108,
1.12383576, 1.17094158, 1.17934022)
gg_b <- ggplot_build(ggplot() + geom_histogram(aes(x = myData), binwidth=1))
nu_bins <- dim(gg_b$data[[1]])[1]
ggplot() + geom_histogram(aes(x = myData), binwidth=1,
fill = rainbow(nu_bins, start=4/6)) + scale_color_gradient2(low='darkgreen', mid='orange', high='red', midpoint=2, name= 'mydata', trans = "log2")
# #scale_color_gradient2 obviously does nothing: How do I get it to work?
The plot should look somewhat like this:
# #Histogram
hp <- qplot(x =myData, fill=..count.., geom="histogram")
hp
hp + scale_fill_gradient2(low="darkgreen", mid="orange", high="red",
midpoint=2, name= 'mydata', trans = "log2")
However, the bars should be coloured according to the continuous variable in myData, NOT according to the counts.
I'm pretty sure I'm missing something obvious, apologies for that. Any help is much appreciated!