0

When I use ggplot and try to change the legend name from "value" to "Work schedules" doesn't change. As well the scale 0 - Did not worked; 1-Did worked. Do you know what could be wrong with my code:

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

phalteman
  • 3,442
  • 1
  • 29
  • 46
Rfanatic
  • 2,224
  • 1
  • 5
  • 21
  • `labs(fill = "Work schedules")` – Jordo82 May 30 '19 at 18:39
  • @Jordo82 thanks but how can I change the name of the scale in the legend? 0 - Not worked; 2 - Worked ; i tried scale_manuel scale_shape_discrete(name=("Work schedules"),breaks=c(2, 0),labels=c("Worked", "Did not worked")) – Rfanatic May 30 '19 at 19:10
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. – camille May 30 '19 at 19:38

1 Answers1

2

@Jordo82 has the right answer for naming the legend. As far as changing the scale from continuous to discrete, you should take a look at your variable "value" and see the range of the (for lack of a better word) values. If the variable type is a double, you may need to use dplyr::mutate() to create ranges. If the values are indeed discrete, try dplyr::mutate(value = as.factor(values))

df3 <- df3 %>% dplyr::mutate(value = ifelse(value < 2, "Not Worked", "Worked"))
rfortin
  • 184
  • 8
  • thank you the code works but it changed the color to red and green how can i change back the colors? – Rfanatic May 30 '19 at 19:45
  • 1
    Try looking up scale_fill_manual. This will allow you to find specific hex codes of colors you like and set them. `ggplot(...) + scale_fill_manual(values = c(HEX_CODE1, HEX_CODE2)` – rfortin May 30 '19 at 20:00