I am trying to make a ggplot where the order of a categorical variable on the y axis is 1) reordered by the value of another variable and then 2) the axis text is swapped for another value where a given level of the new labels could have more than one corresponding value from the original variable. Here's a reproducible example accomplishing the first part:
df <- data.frame(id = rep(letters[1:5], each=10),
id_2 = rep(c('group1', 'group2', 'group1', 'group3', 'group4'), each=10),
date = rep(seq(0, 9), 5),
start_point = rep(rnorm(5, 0, 1), each=10),
change_rate = rep(rnorm(5, 0, 1), each=10),
variance = rnorm(50, 0, 1))
df$fill_val <- df$start_point + df$date*df$change_rate + df$variance
ggplot(df, aes(x=date, y=reorder(id, fill_val), fill=fill_val)) +
geom_tile()
This produces:
Now, I would like to replace the labels on the y axis with the values found in id_2. Note that you could of course do this manually in this toy example like so:
ggplot(df, aes(x=date, y=reorder(id, fill_val), fill=fill_val)) +
geom_tile() +
scale_y_discrete(breaks=c('d', 'b', 'a', 'e', 'c'),
labels=c('group3', 'group2', 'group1', 'group4', 'group1'))
producing:
But with the real data, I need to do this repeatedly, and in some plots there may be dozens of values on the y axis, so it can't be a manual fix as above.



