将渐变图例的比例更改为具有特定中断的百分比

C8H*_*4O2 4 r ggplot2

我有一个带有渐变的热图,我想以特定百分比标记图例。

# 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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

C8H*_*4O2 9

我需要设置breakslabelsscale_fill_gradient()

+ scale_fill_gradient(low='yellow',high='brown', 
                      name='% of Cyl. Group', 
                      breaks = 0.25*0:4, labels = percent(0.25*0:4) ) # <-
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明