how to write a color condition on R?

Viewed 52

I'm working on the flowering of maize. So here is my data.

X2 <- structure(list(Cumulatif.mâle = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), name = c("date.mâle.graph", "ASIi.floraisons.mâles", "date.mâle.graph", "ASIi.floraisons.mâles", "date.mâle.graph", "ASIi.floraisons.mâles", "date.mâle.graph", "ASIi.floraisons.mâles", "date.mâle.graph", "ASIi.floraisons.mâles"), value = c(29, 41, 32, 42, 33, 48, 34, 56, 35, 48)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))


ggplot(X2, aes(x = value, y = Cumulatif.mâle, color = name)) + geom_point()+ geom_line(
  data = subset(X2, name == "date.mâle.graph"),
  inherit.aes = TRUE,
  size = 1) + ggtitle("Floraison mâle en fonction du temps et de leurs ASIi")+ xlab("Floraison mâle") + ylab("Individus de la variété") + xlim(range(pretty(X2$value))) + scale_color_manual(values = c("blue", "black")) + theme_minimal()+ theme(
  plot.title = element_text(color="black", size=14, face="plain"),
  axis.title = element_text(color="black", size=16, face="plain"),
  axis.text = element_text(face="bold", color="#993333", size=14, angle=0))

This graph allow me to visualize the male flowering in function of time. But futhermore this graph allow me to visualize the ASIi (It's a data that interest me a lot) in function of the male flowering. You can see the graph below.

enter image description here

So here is my issue: I would like to visualize the ASIi, so the datas named "ASIi.floraisons.mâles" but with a point color code like this:

green: ASIi is inferior or egal at 3                 
orange: ASIi is  comprised between 4 and 6                      
red: ASIi is  comprised between 7 and 9                         
dark: ASIi is superior or egal at 10    

This color code will be very really visual and really helpful in my work.

Do you have a idea how can I do?

1 Answers

First I would create a Color column to characterize each point with the color you want according to its value. Then you can take your code back but don't forget to remove + scale_color_manual(values = c("blue", "black")) and to add scale_color_identity() :

X2 <- structure(list(Cumulatif.mâle = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), name = c("date.mâle.graph", "ASIi.floraisons.mâles", "date.mâle.graph", "ASIi.floraisons.mâles", "date.mâle.graph", "ASIi.floraisons.mâles", "date.mâle.graph", "ASIi.floraisons.mâles", "date.mâle.graph", "ASIi.floraisons.mâles"), value = c(29, 41, 32, 42, 33, 48, 34, 56, 35, 48)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

X2=X2%>% arrange(Cumulatif.mâle, desc(name)) %>% mutate(Color = case_when(name == 'date.mâle.graph' ~ value, TRUE ~ value - lag(value)))%>%
mutate(Color=ifelse(name=="date.mâle.graph","black",
                            ifelse(Color<=3,"green",
                                   ifelse((Color>=4)&(Color<=6),"orange",
                                          ifelse((Color>=7)&(Color<=9),"red","blue")))))

ggplot(X2,aes(x = value, y = Cumulatif.mâle, color = Color)) + geom_point()+ geom_line(
  data = subset(X2, name == "date.mâle.graph"),
  inherit.aes = TRUE,
  size = 1) + ggtitle("Floraison mâle en fonction du temps et de leurs ASIi")+ xlab("Floraison mâle") + ylab("Individus de la variété") + xlim(range(pretty(X2$value))) + theme_minimal()+ theme(
    plot.title = element_text(color="black", size=14, face="plain"),
    axis.title = element_text(color="black", size=16, face="plain"),
    axis.text = element_text(face="bold", color="#993333", size=14, angle=0))+
  scale_color_identity()

Just pay attention there is no color "dark" so I replaced it by "blue" in this exemple

Related