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

时间:2015-11-04 21:51:19

标签: 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()

enter image description here

1 个答案:

答案 0 :(得分:4)

我需要在breaks中设置labelsscale_fill_gradient()

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

enter image description here