如何更改ggplot的图例

时间:2019-05-30 18:18:31

标签: r dataframe ggplot2 legend

当我使用ggplot并尝试将图例名称从“值”更改为“工作计划”时,不会更改。以及0级-没用; 1-努力了。您知道我的代码有什么问题吗?

plot <- ggplot(df3, aes(x = time, y = index, fill = value)) + 
  geom_raster() + 
  facet_grid(~ day) + 
  theme(panel.spacing = unit(1, "mm"), 
        axis.text.x = element_text(angle = 90, hjust = 1)) + 
  labs(x="Hours", y ="Identification Number") + 
  scale_x_continuous(breaks =  c(9,17), name= "Time") + 
  scale_y_continuous() 

plot + annotate("rect", fill = "red", alpha = 0.5, xmin = c(9), xmax = c(17), ymin = -Inf, ymax = Inf) + 
  ylab ("Identification number") + 
  theme_bw()

enter image description here

1 个答案:

答案 0 :(得分:2)

@ Jordo82为命名图例提供了正确的答案。至于将标度从连续更改为离散,则应查看变量“值”,并查看(缺少更好的单词)值的范围。如果变量类型是双精度型,则可能需要使用dplyr :: mutate()创建范围。如果这些值确实是离散的,请尝试dplyr :: mutate(value = as.factor(values))

df3 <- df3 %>% dplyr::mutate(value = ifelse(value < 2, "Not Worked", "Worked"))