I want a heatmap that is shown in figure A but what I get actually is shown in figure B. Figure A is not a heatmap, actually it is a ribbon3d map, but it has a problem of text3D on x axis. So, I decided to use a heatmap, but heatmap has a problem. It automatically arranges the y axis labels based on aphabatical order. I want it to arrange by the weihted mean across year. I am putting data and figures.
structure(list(Product = c("Petroleum oils, oils from bitumin. materials, crude",
"Petroleum oils, oils from bitumin. materials, crude", "Petroleum oils, oils from bitumin. materials, crude",
"Petroleum oils, oils from bitumin. materials, crude", "Petroleum oils, oils from bitumin. materials, crude",
"Petroleum oils, oils from bitumin. materials, crude", "Petroleum oils, oils from bitumin. materials, crude",
"Petroleum oils, oils from bitumin. materials, crude", "Petroleum oils, oils from bitumin. materials, crude",
"Petroleum oils, oils from bitumin. materials, crude", "Petroleum oils, oils from bitumin. materials, crude",
"Petroleum oils, oils from bitumin. materials, crude", "Furniture & parts",
"Manufactures of base metal, n.e.s.", "Furniture & parts", "Manufactures of base metal, n.e.s.",
"Furniture & parts", "Manufactures of base metal, n.e.s.", "Furniture & parts",
"Manufactures of base metal, n.e.s.", "Furniture & parts", "Manufactures of base metal, n.e.s.",
"Furniture & parts", "Manufactures of base metal, n.e.s.", "Furniture & parts",
"Manufactures of base metal, n.e.s.", "Furniture & parts", "Manufactures of base metal, n.e.s.",
"Furniture & parts", "Manufactures of base metal, n.e.s.", "Furniture & parts",
"Manufactures of base metal, n.e.s.", "Furniture & parts", "Manufactures of base metal, n.e.s.",
"Furniture & parts", "Manufactures of base metal, n.e.s.", "Apparatus for electrical circuits; board, panels",
"Apparatus for electrical circuits; board, panels", "Apparatus for electrical circuits; board, panels",
"Apparatus for electrical circuits; board, panels", "Apparatus for electrical circuits; board, panels",
"Apparatus for electrical circuits; board, panels", "Apparatus for electrical circuits; board, panels",
"Apparatus for electrical circuits; board, panels", "Apparatus for electrical circuits; board, panels",
"Apparatus for electrical circuits; board, panels", "Apparatus for electrical circuits; board, panels",
"Apparatus for electrical circuits; board, panels", "Railway vehicles & associated equipment",
"Railway vehicles & associated equipment", "Railway vehicles & associated equipment",
"Railway vehicles & associated equipment", "Railway vehicles & associated equipment",
"Railway vehicles & associated equipment", "Railway vehicles & associated equipment",
"Railway vehicles & associated equipment", "Railway vehicles & associated equipment",
"Railway vehicles & associated equipment", "Railway vehicles & associated equipment",
"Railway vehicles & associated equipment", "Structures & parts, n.e.s., of iron, steel, aluminium",
"Structures & parts, n.e.s., of iron, steel, aluminium", "Structures & parts, n.e.s., of iron, steel, aluminium",
"Structures & parts, n.e.s., of iron, steel, aluminium", "Structures & parts, n.e.s., of iron, steel, aluminium",
"Structures & parts, n.e.s., of iron, steel, aluminium", "Structures & parts, n.e.s., of iron, steel, aluminium",
"Structures & parts, n.e.s., of iron, steel, aluminium", "Structures & parts, n.e.s., of iron, steel, aluminium",
"Structures & parts, n.e.s., of iron, steel, aluminium", "Structures & parts, n.e.s., of iron, steel, aluminium",
"Structures & parts, n.e.s., of iron, steel, aluminium", "Footwear",
"Footwear", "Footwear", "Footwear", "Footwear", "Footwear", "Footwear",
"Footwear", "Footwear", "Footwear", "Footwear", "Footwear", "Other machinery for particular industries, n.e.s.",
"Other machinery for particular industries, n.e.s.", "Other machinery for particular industries, n.e.s.",
"Other machinery for particular industries, n.e.s.", "Other machinery for particular industries, n.e.s.",
"Other machinery for particular industries, n.e.s.", "Other machinery for particular industries, n.e.s.",
"Other machinery for particular industries, n.e.s.", "Other machinery for particular industries, n.e.s.",
"Other machinery for particular industries, n.e.s.", "Other machinery for particular industries, n.e.s.",
"Other machinery for particular industries, n.e.s.", "Pumps (excluding liquid), gas compressors & fans; centr.",
"Pumps (excluding liquid), gas compressors & fans; centr.", "Pumps (excluding liquid), gas compressors & fans; centr.",
"Pumps (excluding liquid), gas compressors & fans; centr.", "Pumps (excluding liquid), gas compressors & fans; centr.",
"Pumps (excluding liquid), gas compressors & fans; centr.", "Pumps (excluding liquid), gas compressors & fans; centr.",
"Pumps (excluding liquid), gas compressors & fans; centr.", "Pumps (excluding liquid), gas compressors & fans; centr.",
"Pumps (excluding liquid), gas compressors & fans; centr.", "Pumps (excluding liquid), gas compressors & fans; centr.",
"Pumps (excluding liquid), gas compressors & fans; centr.", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Aircraft & associated equipment; spacecraft, etc.",
"Iron & steel bars, rods, angles, shapes & sections", "Heating & cooling equipment & parts thereof, n.e.s.",
"Heating & cooling equipment & parts thereof, n.e.s.", "Heating & cooling equipment & parts thereof, n.e.s.",
"Heating & cooling equipment & parts thereof, n.e.s.", "Heating & cooling equipment & parts thereof, n.e.s.",
"Heating & cooling equipment & parts thereof, n.e.s.", "Heating & cooling equipment & parts thereof, n.e.s.",
"Heating & cooling equipment & parts thereof, n.e.s.", "Heating & cooling equipment & parts thereof, n.e.s.",
"Heating & cooling equipment & parts thereof, n.e.s.", "Heating & cooling equipment & parts thereof, n.e.s.",
"Heating & cooling equipment & parts thereof, n.e.s.", "Motor vehic. for transport of goods, special purpo.",
"Motor vehic. for transport of goods, special purpo.", "Motor vehic. for transport of goods, special purpo.",
"Motor vehic. for transport of goods, special purpo.", "Motor vehic. for transport of goods, special purpo.",
"Motor vehic. for transport of goods, special purpo.", "Motor vehic. for transport of goods, special purpo.",
"Motor vehic. for transport of goods, special purpo.", "Motor vehic. for transport of goods, special purpo.",
"Motor vehic. for transport of goods, special purpo.", "Motor vehic. for transport of goods, special purpo.",
"Motor vehic. for transport of goods, special purpo.", "Parts & accessories of vehicles of 722, 781, 782, 783",
"Parts & accessories of vehicles of 722, 781, 782, 783", "Parts & accessories of vehicles of 722, 781, 782, 783",
"Parts & accessories of vehicles of 722, 781, 782, 783", "Parts & accessories of vehicles of 722, 781, 782, 783",
"Parts & accessories of vehicles of 722, 781, 782, 783", "Parts & accessories of vehicles of 722, 781, 782, 783",
"Parts & accessories of vehicles of 722, 781, 782, 783", "Parts & accessories of vehicles of 722, 781, 782, 783",
"Parts & accessories of vehicles of 722, 781, 782, 783", "Parts & accessories of vehicles of 722, 781, 782, 783",
"Parts & accessories of vehicles of 722, 781, 782, 783", "Telecommunication equipment, n.e.s.; & parts, n.e.s.",
"Telecommunication equipment, n.e.s.; & parts, n.e.s.", "Telecommunication equipment, n.e.s.; & parts, n.e.s.",
"Telecommunication equipment, n.e.s.; & parts, n.e.s.", "Telecommunication equipment, n.e.s.; & parts, n.e.s.",
"Telecommunication equipment, n.e.s.; & parts, n.e.s.", "Telecommunication equipment, n.e.s.; & parts, n.e.s.",
"Telecommunication equipment, n.e.s.; & parts, n.e.s.", "Telecommunication equipment, n.e.s.; & parts, n.e.s.",
"Telecommunication equipment, n.e.s.; & parts, n.e.s.", "Telecommunication equipment, n.e.s.; & parts, n.e.s.",
"Telecommunication equipment, n.e.s.; & parts, n.e.s.", "Medicaments (incl. veterinary medicaments)",
"Medicaments (incl. veterinary medicaments)", "Medicaments (incl. veterinary medicaments)",
"Medicaments (incl. veterinary medicaments)", "Medicaments (incl. veterinary medicaments)",
"Medicaments (incl. veterinary medicaments)", "Medicaments (incl. veterinary medicaments)",
"Medicaments (incl. veterinary medicaments)", "Medicaments (incl. veterinary medicaments)",
"Medicaments (incl. veterinary medicaments)", "Medicaments (incl. veterinary medicaments)",
"Medicaments (incl. veterinary medicaments)", "Motor vehicles for the transport of persons",
"Motor vehicles for the transport of persons", "Motor vehicles for the transport of persons",
"Motor vehicles for the transport of persons", "Motor vehicles for the transport of persons",
"Motor vehicles for the transport of persons", "Motor vehicles for the transport of persons",
"Motor vehicles for the transport of persons", "Motor vehicles for the transport of persons",
"Motor vehicles for the transport of persons", "Motor vehicles for the transport of persons",
"Motor vehicles for the transport of persons", "Civil engineering & contractors' plant & equipment",
"Civil engineering & contractors' plant & equipment", "Civil engineering & contractors' plant & equipment",
"Civil engineering & contractors' plant & equipment", "Civil engineering & contractors' plant & equipment",
"Civil engineering & contractors' plant & equipment", "Civil engineering & contractors' plant & equipment",
"Civil engineering & contractors' plant & equipment", "Civil engineering & contractors' plant & equipment",
"Civil engineering & contractors' plant & equipment", "Civil engineering & contractors' plant & equipment",
"Civil engineering & contractors' plant & equipment", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel", "Petroleum oils or bituminous minerals > 70 % oil",
"Tubes, pipes & hollow profiles, fittings, iron, steel"), Mean = c(59.0833333333333,
59.0833333333333, 59.0833333333333, 59.0833333333333, 59.0833333333333,
59.0833333333333, 59.0833333333333, 59.0833333333333, 59.0833333333333,
59.0833333333333, 59.0833333333333, 59.0833333333333, 59.5, 59.5,
59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5,
59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5, 59.5,
60.3333333333333, 60.3333333333333, 60.3333333333333, 60.3333333333333,
60.3333333333333, 60.3333333333333, 60.3333333333333, 60.3333333333333,
60.3333333333333, 60.3333333333333, 60.3333333333333, 60.3333333333333,
62.6666666666667, 62.6666666666667, 62.6666666666667, 62.6666666666667,
62.6666666666667, 62.6666666666667, 62.6666666666667, 62.6666666666667,
62.6666666666667, 62.6666666666667, 62.6666666666667, 62.6666666666667,
62.8333333333333, 62.8333333333333, 62.8333333333333, 62.8333333333333,
62.8333333333333, 62.8333333333333, 62.8333333333333, 62.8333333333333,
62.8333333333333, 62.8333333333333, 62.8333333333333, 62.8333333333333,
64.75, 64.75, 64.75, 64.75, 64.75, 64.75, 64.75, 64.75, 64.75,
64.75, 64.75, 64.75, 65.25, 65.25, 65.25, 65.25, 65.25, 65.25,
65.25, 65.25, 65.25, 65.25, 65.25, 65.25, 66.5833333333333, 66.5833333333333,
66.5833333333333, 66.5833333333333, 66.5833333333333, 66.5833333333333,
66.5833333333333, 66.5833333333333, 66.5833333333333, 66.5833333333333,
66.5833333333333, 66.5833333333333, 66.75, 66.75, 66.75, 66.75,
66.75, 66.75, 66.75, 66.75, 66.75, 66.75, 66.75, 66.75, 66.75,
66.75, 66.75, 66.75, 66.75, 66.75, 66.75, 66.75, 66.75, 66.75,
66.75, 66.75, 67.25, 67.25, 67.25, 67.25, 67.25, 67.25, 67.25,
67.25, 67.25, 67.25, 67.25, 67.25, 71.0833333333333, 71.0833333333333,
71.0833333333333, 71.0833333333333, 71.0833333333333, 71.0833333333333,
71.0833333333333, 71.0833333333333, 71.0833333333333, 71.0833333333333,
71.0833333333333, 71.0833333333333, 71.9166666666667, 71.9166666666667,
71.9166666666667, 71.9166666666667, 71.9166666666667, 71.9166666666667,
71.9166666666667, 71.9166666666667, 71.9166666666667, 71.9166666666667,
71.9166666666667, 71.9166666666667, 72.75, 72.75, 72.75, 72.75,
72.75, 72.75, 72.75, 72.75, 72.75, 72.75, 72.75, 72.75, 73.25,
73.25, 73.25, 73.25, 73.25, 73.25, 73.25, 73.25, 73.25, 73.25,
73.25, 73.25, 73.4166666666667, 73.4166666666667, 73.4166666666667,
73.4166666666667, 73.4166666666667, 73.4166666666667, 73.4166666666667,
73.4166666666667, 73.4166666666667, 73.4166666666667, 73.4166666666667,
73.4166666666667, 75.0833333333333, 75.0833333333333, 75.0833333333333,
75.0833333333333, 75.0833333333333, 75.0833333333333, 75.0833333333333,
75.0833333333333, 75.0833333333333, 75.0833333333333, 75.0833333333333,
75.0833333333333, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78), year = c("2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2005", "2005", "2006", "2006", "2007",
"2007", "2008", "2008", "2009", "2009", "2010", "2010", "2011",
"2011", "2012", "2012", "2013", "2013", "2014", "2014", "2015",
"2015", "2016", "2016", "2005", "2006", "2007", "2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2005", "2006", "2007", "2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2005", "2006", "2007", "2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2005", "2005", "2006", "2006", "2007",
"2007", "2008", "2008", "2009", "2009", "2010", "2010", "2011",
"2011", "2012", "2012", "2013", "2013", "2014", "2014", "2015",
"2015", "2016", "2016", "2005", "2006", "2007", "2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2005", "2006", "2007", "2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2005", "2006", "2007", "2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2005", "2006", "2007", "2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2005",
"2005", "2006", "2006", "2007", "2007", "2008", "2008", "2009",
"2009", "2010", "2010", "2011", "2011", "2012", "2012", "2013",
"2013", "2014", "2014", "2015", "2015", "2016", "2016"), score2 = c(456533,
493039, 493039, 474552, 474552, 421875, 474552, 456533, 456533,
729, 1, 1, 238328, 175616, 274625, 132651, 300763, 157464, 195112,
205379, 195112, 238328, 175616, 226981, 185193, 205379, 195112,
238328, 195112, 216000, 216000, 238328, 205379, 250047, 175616,
274625, 195112, 148877, 205379, 175616, 287496, 314432, 226981,
175616, 185193, 250047, 262144, 250047, 328509, 300763, 185193,
262144, 216000, 343000, 389017, 438976, 343000, 274625, 54872,
79507, 148877, 250047, 216000, 300763, 373248, 262144, 175616,
185193, 205379, 262144, 343000, 328509, 274625, 195112, 110592,
140608, 226981, 300763, 300763, 314432, 314432, 389017, 421875,
421875, 300763, 287496, 314432, 287496, 300763, 250047, 195112,
226981, 262144, 343000, 287496, 300763, 216000, 238328, 262144,
274625, 438976, 287496, 287496, 287496, 274625, 287496, 300763,
438976, 262144, 314432, 357911, 343000, 328509, 389017, 343000,
357911, 262144, 250047, 357911, 185193, 314432, 262144, 216000,
328509, 250047, 328509, 328509, 314432, 238328, 314432, 343000,
226981, 287496, 314432, 274625, 389017, 274625, 216000, 274625,
300763, 300763, 300763, 357911, 389017, 389017, 405224, 405224,
373248, 357911, 328509, 343000, 389017, 389017, 357911, 328509,
262144, 357911, 373248, 357911, 328509, 389017, 405224, 357911,
357911, 373248, 405224, 389017, 373248, 405224, 389017, 373248,
405224, 328509, 389017, 421875, 343000, 357911, 373248, 438976,
405224, 343000, 328509, 343000, 314432, 405224, 456533, 405224,
373248, 421875, 438976, 456533, 456533, 421875, 438976, 438976,
421875, 314432, 373248, 373248, 421875, 438976, 474552, 373248,
287496, 438976, 421875, 421875, 438976, 456533, 474552, 438976,
405224, 405224, 421875, 405224, 357911, 474552, 493039, 474552,
456533, 456533, 474552, 456533, 493039, 421875, 493039, 493039,
438976, 493039, 456533, 474552, 493039, 474552, 493039, 493039,
456533, 493039, 474552, 493039, 474552)), row.names = c(NA, -240L
), groups = structure(list(Product = c("Aircraft & associated equipment; spacecraft, etc.",
"Apparatus for electrical circuits; board, panels", "Civil engineering & contractors' plant & equipment",
"Footwear", "Furniture & parts", "Heating & cooling equipment & parts thereof, n.e.s.",
"Iron & steel bars, rods, angles, shapes & sections", "Manufactures of base metal, n.e.s.",
"Medicaments (incl. veterinary medicaments)", "Motor vehic. for transport of goods, special purpo.",
"Motor vehicles for the transport of persons", "Other machinery for particular industries, n.e.s.",
"Parts & accessories of vehicles of 722, 781, 782, 783", "Petroleum oils or bituminous minerals > 70 % oil",
"Petroleum oils, oils from bitumin. materials, crude", "Pumps (excluding liquid), gas compressors & fans; centr.",
"Railway vehicles & associated equipment", "Structures & parts, n.e.s., of iron, steel, aluminium",
"Telecommunication equipment, n.e.s.; & parts, n.e.s.", "Tubes, pipes & hollow profiles, fittings, iron, steel"
), Mean = c(66.75, 60.3333333333333, 75.0833333333333, 64.75,
59.5, 67.25, 66.75, 59.5, 73.25, 71.0833333333333, 73.4166666666667,
65.25, 71.9166666666667, 78, 59.0833333333333, 66.5833333333333,
62.6666666666667, 62.8333333333333, 72.75, 78), .rows = list(
c(109L, 111L, 113L, 115L, 117L, 119L, 121L, 123L, 125L, 127L,
129L, 131L), 37:48, 205:216, 73:84, c(13L, 15L, 17L, 19L,
21L, 23L, 25L, 27L, 29L, 31L, 33L, 35L), 133:144, c(110L,
112L, 114L, 116L, 118L, 120L, 122L, 124L, 126L, 128L, 130L,
132L), c(14L, 16L, 18L, 20L, 22L, 24L, 26L, 28L, 30L, 32L,
34L, 36L), 181:192, 145:156, 193:204, 85:96, 157:168, c(217L,
219L, 221L, 223L, 225L, 227L, 229L, 231L, 233L, 235L, 237L,
239L), 1:12, 97:108, 49:60, 61:72, 169:180, c(218L, 220L,
222L, 224L, 226L, 228L, 230L, 232L, 234L, 236L, 238L, 240L
))), row.names = c(NA, -20L), class = c("tbl_df", "tbl",
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"))
I am using the following code:
library(ggplot2)
ggplot(casx6, aes(Product, year)) + geom_tile(aes(fill = score2), colour = "white") +
scale_fill_gradient2(low = "red", high = "blue") + coord_flip() + guides(fill = F) +
labs(title = "Fig A. Central Asia's Leading Imports") +
theme(axis.title = element_blank(), axis.text.y = element_text(size = 12),
axis.text.x = element_text(size = 12))
You can reorder()
based on Mean
inside of aes()
:
library(ggplot2)
ggplot(casx6, aes(reorder(Product, Mean, mean), year)) +
geom_tile(aes(fill = score2), colour = "white") +
scale_fill_gradient2(low = "red", high = "blue") +
coord_flip() + guides(fill = F) +
labs(title = "Fig A. Central Asia's Leading Imports") +
theme(axis.title = element_blank(), axis.text.y = element_text(size = 12),
axis.text.x = element_text(size = 12))
Created on 2019-07-01 by the reprex package (v0.3.0)