larmor
larmor

Reputation: 1

force legend in geom_tile (ggplot, R)

I don't manage to add a continuous legend to a ggplot using geom_tile.

I had to manipulate the layer_data (for various reasons), hence obtaining a dataframe of the type:

data =
      fill x  y PANEL group xmin xmax ymin ymax colour size linetype alpha width height
1  #C4FFB1 1 19     1    19  0.5  1.5 18.5 19.5     NA  0.1        1    NA    NA     NA
2  #E7FFDE 1 20     1    20  0.5  1.5 19.5 20.5     NA  0.1        1    NA    NA     NA
3  #FAFFF8 1  1     1     1  0.5  1.5  0.5  1.5     NA  0.1        1    NA    NA     NA
4  #C7FFB5 1 13     1    13  0.5  1.5 12.5 13.5     NA  0.1        1    NA    NA     NA
5  #DFFFD3 1 10     1    10  0.5  1.5  9.5 10.5     NA  0.1        1    NA    NA     NA
6  #A3FF8B 1 15     1    15  0.5  1.5 14.5 15.5     NA  0.1        1    NA    NA     NA
7  #FAFFF8 1 18     1    18  0.5  1.5 17.5 18.5     NA  0.1        1    NA    NA     NA
8  #CEFFBC 1 17     1    17  0.5  1.5 16.5 17.5     NA  0.1        1    NA    NA     NA
9  #F5FFF0 1  5     1     5  0.5  1.5  4.5  5.5     NA  0.1        1    NA    NA     NA
10 #D9FFCB 1 25     1    25  0.5  1.5 24.5 25.5     NA  0.1        1    NA    NA     NA
11 #D3FFC4 1  4     1     4  0.5  1.5  3.5  4.5     NA  0.1        1    NA    NA     NA
12 #76FF5D 1 24     1    24  0.5  1.5 23.5 24.5     NA  0.1        1    NA    NA     NA
13 #5AFF44 1 23     1    23  0.5  1.5 22.5 23.5     NA  0.1        1    NA    NA     NA
14 #E4FFDA 1  9     1     9  0.5  1.5  8.5  9.5     NA  0.1        1    NA    NA     NA
15 #C1FFAD 1 16     1    16  0.5  1.5 15.5 16.5     NA  0.1        1    NA    NA     NA
16 #C4FFB1 1 14     1    14  0.5  1.5 13.5 14.5     NA  0.1        1    NA    NA     NA
17 #E4FFDA 1  2     1     2  0.5  1.5  1.5  2.5     NA  0.1        1    NA    NA     NA
18 #FFFFFF 1 12     1    12  0.5  1.5 11.5 12.5     NA  0.1        1    NA    NA     NA
19 #D6FFC8 1  6     1     6  0.5  1.5  5.5  6.5     NA  0.1        1    NA    NA     NA
20 #9CFF83 1 22     1    22  0.5  1.5 21.5 22.5     NA  0.1        1    NA    NA     NA
21 #DFFFD3 1 11     1    11  0.5  1.5 10.5 11.5     NA  0.1        1    NA    NA     NA
22 #DFFFD3 1  8     1     8  0.5  1.5  7.5  8.5     NA  0.1        1    NA    NA     NA
23 #C7FFB5 1 21     1    21  0.5  1.5 20.5 21.5     NA  0.1        1    NA    NA     NA
24 #CEFFBC 1  7     1     7  0.5  1.5  6.5  7.5     NA  0.1        1    NA    NA     NA
25 #FAFFF8 1  3     1     3  0.5  1.5  2.5  3.5     NA  0.1        1    NA    NA     NA
26 #A87FFF 1 27     1    27  0.5  1.5 26.5 27.5     NA  0.1        1    NA    NA     NA
27 #FF6140 1 26     1    26  0.5  1.5 25.5 26.5     NA  0.1        1    NA    NA     NA
28 #C7FFB5 2 19     1    46  1.5  2.5 18.5 19.5     NA  0.1        1    NA    NA     NA
29 #E2FFD6 2 20     1    47  1.5  2.5 19.5 20.5     NA  0.1        1    NA    NA     NA
30 #FFFFFF 2  1     1    28  1.5  2.5  0.5  1.5     NA  0.1        1    NA    NA     NA
31 #CEFFBC 2 13     1    40  1.5  2.5 12.5 13.5     NA  0.1        1    NA    NA     NA
32 #DFFFD3 2 10     1    37  1.5  2.5  9.5 10.5     NA  0.1        1    NA    NA     NA
33 #DFFFD3 2 15     1    42  1.5  2.5 14.5 15.5     NA  0.1        1    NA    NA     NA
34 #CBFFB9 2 18     1    45  1.5  2.5 17.5 18.5     NA  0.1        1    NA    NA     NA
35 #CEFFBC 2 17     1    44  1.5  2.5 16.5 17.5     NA  0.1        1    NA    NA     NA
36 #F5FFF0 2  5     1    32  1.5  2.5  4.5  5.5     NA  0.1        1    NA    NA     NA
37 #C4FFB1 2 25     1    52  1.5  2.5 24.5 25.5     NA  0.1        1    NA    NA     NA
38 #E2FFD6 2  4     1    31  1.5  2.5  3.5  4.5     NA  0.1        1    NA    NA     NA
39 #B4FF9E 2 24     1    51  1.5  2.5 23.5 24.5     NA  0.1        1    NA    NA     NA
40 #DCFFCF 2 23     1    50  1.5  2.5 22.5 23.5     NA  0.1        1    NA    NA     NA
41 #FFFFFF 2  9     1    36  1.5  2.5  8.5  9.5     NA  0.1        1    NA    NA     NA
42 #D3FFC4 2 16     1    43  1.5  2.5 15.5 16.5     NA  0.1        1    NA    NA     NA
43 #E7FFDE 2 14     1    41  1.5  2.5 13.5 14.5     NA  0.1        1    NA    NA     NA
44 #FCFFFB 2  2     1    29  1.5  2.5  1.5  2.5     NA  0.1        1    NA    NA     NA
45 #EFFFE9 2 12     1    39  1.5  2.5 11.5 12.5     NA  0.1        1    NA    NA     NA
46 #EAFFE1 2  6     1    33  1.5  2.5  5.5  6.5     NA  0.1        1    NA    NA     NA
47 #E2FFD6 2 22     1    49  1.5  2.5 21.5 22.5     NA  0.1        1    NA    NA     NA
48 #FAFFF8 2 11     1    38  1.5  2.5 10.5 11.5     NA  0.1        1    NA    NA     NA
49 #F5FFF0 2  8     1    35  1.5  2.5  7.5  8.5     NA  0.1        1    NA    NA     NA
50 #D3FFC4 2 21     1    48  1.5  2.5 20.5 21.5     NA  0.1        1    NA    NA     NA
51 #D9FFCB 2  7     1    34  1.5  2.5  6.5  7.5     NA  0.1        1    NA    NA     NA
52 #EAFFE1 2  3     1    30  1.5  2.5  2.5  3.5     NA  0.1        1    NA    NA     NA
53 #9669FF 2 27     1    54  1.5  2.5 26.5 27.5     NA  0.1        1    NA    NA     NA
54 #FF7F5E 2 26     1    53  1.5  2.5 25.5 26.5     NA  0.1        1    NA    NA     NA

When I create my ggplot from this dataframe

ggplot(data , aes(x , y , fill = I(fill))) + geom_tile()

no legend shows up and no amount of scale_fill_gradient() or scale_color_gradient() helps so far.

exemple that doesn't create a legend:

ggplot(gp , aes(x , y , fill = I(fill))) + geom_tile() +
  scale_fill_gradient(name='',low = "white",
                      high = "green",
                      na.value = "grey50",
                      guide = "colourbar",
                      aesthetics = "colour") 

Is there a way of forcing a legend in such a case?

EDIT: Essentially, I want a figure of this type with a legend (at least the green one, but ideally for the three color gradients).

enter image description here

Thanks!

Upvotes: 0

Views: 527

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174616

If I am understanding you correctly, you want the fill colors interpreted literally, so you need scale_fill_identity. By default this does not have a legend, so you need to add guide = 'legend' to the call:

ggplot(data , aes(x , y , fill = fill)) + 
  geom_tile() +
  scale_fill_identity(guide = 'legend')

enter image description here

You have said that you want a continuous scale, but this is harder, since it is not clear what your continuous scale is supposed to represent, nor what numbers should be on it. To some extent you could back-transform the colors, since I am guessing your initial fills went from pure green to pure white. This means that you can back-transform the colors into a numerical value between 0 and 1:

ggplot(data , aes(x , y , fill = (colSums(col2rgb(fill))-255)/510)) + 
  geom_tile() +
  scale_fill_gradient(limits = c(0, 1), name = NULL,
                      low = 'green', high = 'white')

enter image description here

Upvotes: 0

Related Questions