我有一个带有渐变的热图,我想以特定的百分比标记图例。
# 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()