Jarad
Jarad

Reputation: 18953

Reorder R GGPlot Heatmap Layout While Preserving Correct Y-axis Labels

I am plotting a cohort analysis with R. I have a cohort triangle plot going but I want to flip the y-axis to sort the labels with "2009-01" at the top (the longest row) and "2010-03" at the bottom (shortest row). Doing so will flip the triangle from ◺ to ◸ which is what I want.

Data for a recreatable example:

melted <- read.table(text="    CohortPeriod variable      value
1              1  2009-01 1.00000000
2              2  2009-01 0.36363636
3              3  2009-01 0.45454545
4              4  2009-01 0.40909091
5              5  2009-01 0.45454545
6              6  2009-01 0.36363636
7              7  2009-01 0.36363636
8              8  2009-01 0.31818182
9              9  2009-01 0.31818182
10            10  2009-01 0.31818182
11            11  2009-01 0.31818182
12            12  2009-01 0.36363636
13            13  2009-01 0.50000000
14            14  2009-01 0.31818182
15            15  2009-01 0.27272727
16             1  2009-02 1.00000000
17             2  2009-02 0.20000000
18             3  2009-02 0.33333333
19             4  2009-02 0.06666667
20             5  2009-02 0.26666667
21             6  2009-02 0.26666667
22             7  2009-02 0.26666667
23             8  2009-02 0.33333333
24             9  2009-02 0.33333333
25            10  2009-02 0.26666667
26            11  2009-02 0.20000000
27            12  2009-02 0.20000000
28            13  2009-02 0.33333333
29            14  2009-02         NA
30            15  2009-02         NA
31             1  2009-03 1.00000000
32             2  2009-03 0.30769231
33             3  2009-03 0.38461538
34             4  2009-03 0.30769231
35             5  2009-03 0.07692308
36             6  2009-03 0.15384615
37             7  2009-03 0.15384615
38             8  2009-03 0.23076923
39             9  2009-03 0.15384615
40            10  2009-03 0.07692308
41            11  2009-03 0.23076923
42            12  2009-03 0.15384615
43            13  2009-03 0.07692308
44            14  2009-03         NA
45            15  2009-03         NA
46             1  2009-04 1.00000000
47             2  2009-04 0.33333333
48             3  2009-04 0.25641026
49             4  2009-04 0.33333333
50             5  2009-04 0.15384615
51             6  2009-04 0.17948718
52             7  2009-04 0.10256410
53             8  2009-04 0.15384615
54             9  2009-04 0.05128205
55            10  2009-04 0.10256410
56            11  2009-04 0.07692308
57            12  2009-04 0.05128205
58            13  2009-04         NA
59            14  2009-04         NA
60            15  2009-04         NA
61             1  2009-05 1.00000000
62             2  2009-05 0.26000000
63             3  2009-05 0.24000000
64             4  2009-05 0.10000000
65             5  2009-05 0.08000000
66             6  2009-05 0.12000000
67             7  2009-05 0.06000000
68             8  2009-05 0.10000000
69             9  2009-05 0.10000000
70            10  2009-05 0.08000000
71            11  2009-05 0.06000000
72            12  2009-05         NA
73            13  2009-05         NA
74            14  2009-05         NA
75            15  2009-05         NA
76             1  2009-06 1.00000000
77             2  2009-06 0.46875000
78             3  2009-06 0.28125000
79             4  2009-06 0.18750000
80             5  2009-06 0.21875000
81             6  2009-06 0.15625000
82             7  2009-06 0.09375000
83             8  2009-06 0.09375000
84             9  2009-06 0.31250000
85            10  2009-06 0.09375000
86            11  2009-06         NA
87            12  2009-06         NA
88            13  2009-06         NA
89            14  2009-06         NA
90            15  2009-06         NA
91             1  2009-07 1.00000000
92             2  2009-07 0.46000000
93             3  2009-07 0.26000000
94             4  2009-07 0.20000000
95             5  2009-07 0.22000000
96             6  2009-07 0.20000000
97             7  2009-07 0.22000000
98             8  2009-07 0.14000000
99             9  2009-07 0.14000000
100           10  2009-07         NA
101           11  2009-07         NA
102           12  2009-07         NA
103           13  2009-07         NA
104           14  2009-07         NA
105           15  2009-07         NA
106            1  2009-08 1.00000000
107            2  2009-08 0.35483871
108            3  2009-08 0.29032258
109            4  2009-08 0.22580645
110            5  2009-08 0.19354839
111            6  2009-08 0.25806452
112            7  2009-08 0.12903226
113            8  2009-08 0.12903226
114            9  2009-08         NA
115           10  2009-08         NA
116           11  2009-08         NA
117           12  2009-08         NA
118           13  2009-08         NA
119           14  2009-08         NA
120           15  2009-08         NA
121            1  2009-09 1.00000000
122            2  2009-09 0.40540541
123            3  2009-09 0.37837838
124            4  2009-09 0.21621622
125            5  2009-09 0.35135135
126            6  2009-09 0.24324324
127            7  2009-09 0.21621622
128            8  2009-09         NA
129            9  2009-09         NA
130           10  2009-09         NA
131           11  2009-09         NA
132           12  2009-09         NA
133           13  2009-09         NA
134           14  2009-09         NA
135           15  2009-09         NA
136            1  2009-10 1.00000000
137            2  2009-10 0.31481481
138            3  2009-10 0.22222222
139            4  2009-10 0.24074074
140            5  2009-10 0.24074074
141            6  2009-10 0.12962963
142            7  2009-10         NA
143            8  2009-10         NA
144            9  2009-10         NA
145           10  2009-10         NA
146           11  2009-10         NA
147           12  2009-10         NA
148           13  2009-10         NA
149           14  2009-10         NA
150           15  2009-10         NA
151            1  2009-11 1.00000000
152            2  2009-11 0.24615385
153            3  2009-11 0.20000000
154            4  2009-11 0.22307692
155            5  2009-11 0.10000000
156            6  2009-11         NA
157            7  2009-11         NA
158            8  2009-11         NA
159            9  2009-11         NA
160           10  2009-11         NA
161           11  2009-11         NA
162           12  2009-11         NA
163           13  2009-11         NA
164           14  2009-11         NA
165           15  2009-11         NA
166            1  2009-12 1.00000000
167            2  2009-12 0.26153846
168            3  2009-12 0.27692308
169            4  2009-12 0.10769231
170            5  2009-12         NA
171            6  2009-12         NA
172            7  2009-12         NA
173            8  2009-12         NA
174            9  2009-12         NA
175           10  2009-12         NA
176           11  2009-12         NA
177           12  2009-12         NA
178           13  2009-12         NA
179           14  2009-12         NA
180           15  2009-12         NA
181            1  2010-01 1.00000000
182            2  2010-01 0.52631579
183            3  2010-01 0.27368421
184            4  2010-01         NA
185            5  2010-01         NA
186            6  2010-01         NA
187            7  2010-01         NA
188            8  2010-01         NA
189            9  2010-01         NA
190           10  2010-01         NA
191           11  2010-01         NA
192           12  2010-01         NA
193           13  2010-01         NA
194           14  2010-01         NA
195           15  2010-01         NA
196            1  2010-02 1.00000000
197            2  2010-02 0.19000000
198            3  2010-02         NA
199            4  2010-02         NA
200            5  2010-02         NA
201            6  2010-02         NA
202            7  2010-02         NA
203            8  2010-02         NA
204            9  2010-02         NA
205           10  2010-02         NA
206           11  2010-02         NA
207           12  2010-02         NA
208           13  2010-02         NA
209           14  2010-02         NA
210           15  2010-02         NA
211            1  2010-03 1.00000000
212            2  2010-03         NA
213            3  2010-03         NA
214            4  2010-03         NA
215            5  2010-03         NA
216            6  2010-03         NA
217            7  2010-03         NA
218            8  2010-03         NA
219            9  2010-03         NA
220           10  2010-03         NA
221           11  2010-03         NA
222           12  2010-03         NA
223           13  2010-03         NA
224           14  2010-03         NA
225           15  2010-03         NA")

The plot code using ggplot:

xlabels <- 1:15
ggplot(melted, aes(x=CohortPeriod, y=variable)) +
  ggtitle('Retention by cohort') +
  theme_bw() +
  xlab('CohortPeriod') +
  ylab('% of Cohort Purchasing') +
  geom_tile(aes(fill = value), color='white') + geom_text(aes(label = round(value, 2)), size=2) +
  scale_x_continuous(breaks = xlabels) +
  scale_fill_gradient(low = 'white', high = '#2d1e3e', space = 'Lab', na.value = 'white') +
  theme(axis.text.x=element_text(angle=0),
        axis.ticks=element_blank(),
        axis.line=element_blank(),
        panel.border=element_blank(),
        panel.grid.major=element_line(color='#eeeeee'))

If I do ggplot(melted, aes(x=CohortPeriod, y=rev(variable))) + ... it does flip the triangle but the y labels don't change with it.

How do I flip the triangle to ◸ while also sorting the y-axis labels correctly?

Upvotes: 0

Views: 5004

Answers (1)

Jack Brookes
Jack Brookes

Reputation: 3830

One way is to simply reorder the levels of the factor used for the variable column.

library(ggplot2)

# reverse factor levels
melted$variable <- factor(melted$variable, levels=rev(levels(melted$variable)))

xlabels <- 1:15
ggplot(melted, aes(x=CohortPeriod, y=variable)) +
  ggtitle('Retention by cohort') +
  theme_bw() +
  xlab('CohortPeriod') +
  ylab('% of Cohort Purchasing') +
  geom_tile(aes(fill = value), color='white') + geom_text(aes(label = round(value, 2)), size=2) +
  scale_x_continuous(breaks = xlabels) +
  scale_fill_gradient(low = 'white', high = '#2d1e3e', space = 'Lab', na.value = 'white') +
  theme(axis.text.x=element_text(angle=0),
        axis.ticks=element_blank(),
        axis.line=element_blank(),
        panel.border=element_blank(),
        panel.grid.major=element_line(color='#eeeeee'))

enter image description here

Upvotes: 4

Related Questions