【发布时间】:2021-05-12 06:58:38
【问题描述】:
我有这样的数据,我想在数据框(颜色列)中指定我的填充颜色。我希望我的图例显示 cut_value 列中的值。当我同时指定标签和中断时,图例就会消失。如果我只包含标签而不包含中断,则图例会显示。但是,我需要指定中断,因为我需要它们在多个图中保持一致,其中数据包括 cut_value 的不同数据范围。如何包含标签和中断并显示填充图例?
library(tidyverse)
df <- data.frame(sample = letters[1:6],
value = c(1,1.5,NA,3,4, 2)) %>%
mutate(cut_value = cut(value, breaks = c(1,2,3,4)),
color = factor(cut_value,
levels = levels(cut_value),
labels = c('darkred', 'orange', 'yellow')),
color = fct_explicit_na(color, na_level = 'grey85'))
ggplot(df, aes(sample, value))+
geom_bar(stat = 'identity', aes(fill = color))+
scale_fill_identity(guide = 'legend',
labels = levels(df$cut_value))
breaks = levels(df$cut_value))
【问题讨论】: