0

I've been looking around on the site to see if there were any previous posts about ordering columns on a heat map with geom_tiles. I am trying to create a heatmap that shows occurrences of certain scales of hydrologic research within different areas in Canada. My original code is shown below.

##Hydrologic Scale Frequency Table## 
data5=read.csv(file.choose())

#load ggplot2

ggplot(data5, aes(Hydrologic, Jurisdiction)) +
geom_tile(aes(x=Hydrologic, y=reorder(Jurisdiction, Rank), fill = Freq), colour = "black") +
geom_text(aes(label=Freq), color = "black") +
scale_fill_gradient(low = "white", high = "#48D1CC")

The code above does not show my columns in an order that makes sense (i.e. NA/Non distinguishable columns at the end).

When I do the following code before the geom_tiles code, it merges all columns into a single column.

col_order <- c("Basin", "Water body", "Watershed", "Non Distinguishable")
data5$Hydrologic <- factor(data5$Hydrologic, levels = col_order)

Does anyone have any advice as to how I can go about fixing my issue?

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
r1c3bowl22
  • 23
  • 6
  • maybe using fct_reorder or fct_relevel is easier look into the forcats cheat sheet – Bruno May 08 '20 at 23:51
  • Reordering any `ggplot2` discrete axis is the same - doesn't matter if it's tiles, bars, boxplots.... the FAQ should explain pretty well. Make sure that your `col_order` values exactly match the existing unique values in your column. – Gregor Thomas May 08 '20 at 23:59

0 Answers0