如何通过x或y在R中订购热图?

时间:2016-05-02 08:16:40

标签: r ggplot2 heatmap

我已经根据城市创建了温度热图。 ggplot默认从Z-A订购城市,而不是我想要的。如何更改代码以便城市可以订购A-Z,如何在ggplot中订购整体x和y?

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)) 

1 个答案:

答案 0 :(得分:0)

我在reorder中添加了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()