I had a heat map with a gradient for which I wanted to label the legend at specific percentages.
# example data, apologies for the kludginess
library('ggplot2'); library('scales'); require('dplyr');
as.data.frame(with(mtcars, table(gear, cyl))) %>%
group_by(cyl) %>%
mutate(pct_of_cyl_class = Freq / sum(Freq)) %>%
ggplot(. ,aes(cyl, gear)) +
geom_tile(aes(fill=pct_of_cyl_class)) +
scale_fill_gradient(low='yellow',high='brown', name='% of Cyl. Group') +
geom_text(aes(label=percent(pct_of_cyl_class))) +
xlab('Cylinder Class') + ylab('Gears') +
ggtitle('Gear Frequency by Cylinder Class') + theme_minimal()
I needed to set breaks
and labels
in scale_fill_gradient()
.
+ scale_fill_gradient(low='yellow',high='brown',
name='% of Cyl. Group',
breaks = 0.25*0:4, labels = percent(0.25*0:4) ) # <-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With