-1

I'm trying to visualize a Bayesian network, and due to the density of lines, I decided to use a luminosity gradient to visualize direction of an edge (dark -> light). However, because of this, I had to manually define colors in a geom_path() statement, and use scale_fill_identity() instead of a scale_fill_gradient() that I would normally use. My current graph looks like this:

enter image description here

What I want to do is add a gradient scale that goes from blue to yellow with user-defined values, so that someone can look at a legend and tell what the weights of all of the links are (they're all unitless). The saturations and luminosities are all different from the original colors, but the hues are the same.

Edit: Here is an example you can work with:

   ggplot(data.frame(x=1),aes(x=x,y=1,color='red',fill='blue'))+
scale_color_identity()+scale_fill_identity()+geom_bar(stat='identity')

How does one insert an arbitrary color gradient scale onto this?

Edit 2: I found a roundabout way of doing this via manual insertion of legends from other plots: Control over legends of multiple layer plot in ggplot2

Are there any less hacky ways of doing this?

Community
  • 1
  • 1
Max Candocia
  • 4,294
  • 35
  • 58
  • Please see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – csgillespie Jun 30 '14 at 14:18
  • The problem is straightforward: Is it possible to add a legend and scale without having to create any mapping elements? I've already used fill for the boxes and scale_color_identity for the lines. That means I cannot add another color-based scale using mapping elements. The `guides(color=colorbar(...))` option won't work because there are no input parameters for any sort of scale, and by default, it requires a continuous variable. There are 200+ lines of code needed to make the graph, but what I am asking is how to put a custom scale. I entered a basic example, but it seems trivial. – Max Candocia Jun 30 '14 at 14:55

1 Answers1

0

To add a legend easily with ggplot, you need a mapping. For your sample

ggplot(data.frame(x=1),aes(x=x,y=1,color="boxcol",fill=1))+
  scale_fill_gradient(name="Fill")+
  scale_colour_manual(name="Color",values=c(boxcol="blue"))+
  geom_bar(stat='identity')

would add a legend for the color and fill. Assuming this example is an accurate representation of what you were actually doing, then this should work. Rather than specifying literal color values in geom_path, you should encode them.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • The reason I have to explicitly define the colors is because I need a luminosity gradient for the lines I'm specifying. There's no RGB or HSV aesthetics that I'm aware of (the latter would definitely be useful in many cases). The alternative would be to use only size, which are much harder to discriminate with the distances involved, or use arrows, which are extremely hard to read on graphs like this. I've found a workaround using grobs, but it seems like an ugly solution. – Max Candocia Jun 30 '14 at 20:59
  • I don't understand why you can't use `scale_colour_gradientn` with a list of all the colors you are setting manually? If you've already created you own ramp, then plug it in. Pass in 256 colors or so to make a smooth transition in your desired color space. – MrFlick Jun 30 '14 at 21:03
  • If you could create a more realistic example of your real problem, it would be easier to give specific advice. It doesn't have to be (and shouldn't be) your entire data and 200 lines of code. It should be minimal, but sufficient to illustrate the problem. – MrFlick Jun 30 '14 at 21:05
  • I need a 2-dimensional color space (hue and luminosity). I already used color mapping (identity) to achieve that via manually assigning color, and the fill mapping is used up for the nodes. If I use a gradient, I will have to create a large number of endpoints for the gradient and a very strange mapping for all of my edges to account for the range of luminosities. – Max Candocia Jul 01 '14 at 01:10