2

I've created a heatmap on temperatures by city. ggplot orders cities by default, from Z-A, not what I want. How can I change the code so that the cities would be ordered A-Z, how overall x and y can be ordered in ggplot ?

ggplot(Cities, aes(x = Month, y = City, fill = AvgTemp, frame = City)) +
  geom_tile(color = "white", size = 0.5) +
  scale_fill_gradient(name = "Average Temperature",low = "blue", high = "red") +
  coord_equal() +
  labs(x = "Month", y = "", title = "Average Temp") +
  theme_tufte() +
  theme(axis.ticks = element_blank()) +
  theme(axis.text = element_text(size = 15)) +
  theme(plot.title = element_text(size = 15)) +
  theme(legend.title = element_text(size = 10)) +
  theme(legend.text = element_text(size = 10)) 
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Read about `factor()` function. – zx8754 May 02 '16 at 08:17
  • Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 May 02 '16 at 08:18

1 Answers1

1

I've included a reorder in your aes(y=).

 ggplot(data=Cities) +
   geom_tile( aes(x=Month, y=reorder(City, AvgTemp, median, order=TRUE), fill = AvgTemp), color = "white", size = 0.5) +
   scale_fill_gradient(name = "Average Temperature",low = "blue", high = "red") +
   coord_equal() +
   labs(x = "Month", y = "", title = "Average Temp") +
   theme_tufte()
rafa.pereira
  • 13,251
  • 6
  • 71
  • 109