【问题标题】:How to change the legend of a ggplot如何更改ggplot的图例
【发布时间】:2019-05-30 18:18:30
【问题描述】:

当我使用 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()

【问题讨论】:

  • labs(fill = "Work schedules")
  • @Jordo82 谢谢,但如何更改图例中的比例名称? 0 - 不工作; 2 - 工作;我试过 scale_manuel scale_shape_discrete(name=("Work schedules"),breaks=c(2, 0),labels=c("Worked", "Dod not working"))
  • See here 提出一个人们可以帮助解决的 R 问题。这包括数据样本、所有必要的代码,以及对您正在尝试做的事情和没有奏效的事情的清晰解释。

标签: r dataframe ggplot2 legend


【解决方案1】:

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

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

【讨论】:

  • 谢谢你的代码工作,但它改变了颜色为红色和绿色我怎样才能改变颜色?
  • 尝试查找 scale_fill_manual。这将允许您找到您喜欢的颜色的特定十六进制代码并设置它们。 ggplot(...) + scale_fill_manual(values = c(HEX_CODE1, HEX_CODE2)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多