Home > Back-end >  ggplot maps colours in wrong order
ggplot maps colours in wrong order

Time:05-05

Creating a graph with several variables in ggplot, I map the colours using scale_colour_manual but the graph and legend applies the wrong colour to the wrong variable and the whole thing is mixed up. I'm not sure what I am doing wrong.

I have 9 variables in my data and plotting just 6 of them. Could this be why?

Here is a sample of my code:

library(ggplot)

csv2 <- read.delim("data.csv", sep=(","), skip=2, header = TRUE) 

colnames(csv2) <-
  c('kp','var1','var2','var3', 'var4', 'var5', 'var6', 'var7', 'var8', 'var9')

n <-  28
split(csv2, factor(sort(rank(row.names(csv2))%%n))) -> splitdata 

list2env(setNames(splitdata,paste0("splitdata",1:28)), environment())

ggplot()   geom_path(data= splitdata1, aes(x=kp, y=var4, colour="var4"))   
  geom_path(data= splitdata1, aes(x=kp, y=var6, colour="var6"))  
  geom_path(data= splitdata1, aes(x=kp, y=var5, colour="var5"))  
  geom_path(data= splitdata1, aes(x=kp, y=var7, colour="var7"))  
  geom_path(data= splitdata1, aes(x=kp, y=var8, colour="var8"))  
  geom_path(data= splitdata1, aes(x=kp, y=var9, colour="var9"))  
  labs(y="Depth (m)", x="KP", title="Track")   
  theme_bw()  
  scale_colour_manual(labels= c("var4", "var6", "var5", "var7", "var8","var9"),
                      values = c("red","red","green", "green", "blue","darkgrey"))  
  theme(axis.text.x=element_text(size=14), axis.title.x = element_text(size=16),
        axis.text.y=element_text(size=14), axis.title.y = element_text(size=16),
        plot.title = element_text(size=20, face="bold", color = "darkblue"))

And I get this: Graph with wrong colours

Essentially, var5 and var6 are in the wrong place according to the colour.

However, if I remove scale_colour_manual, I get the colours where I want them (outside of aes()), but then I lose the legend. What can I do to solve this?

Here is an example of the data:

> head(splitdata1)
       kp var1 var2 var3   var4   var5    var6    var7   var8   var9
1 101.699 0.01 0.01 0.00 -93.46 -93.47 -92.713 -92.723 -93.33 -93.33
2 101.700 0.01 0.01 0.01 -93.47 -93.48 -92.723 -92.733 -93.34 -93.35
3 101.701 0.02 0.02 0.01 -93.47 -93.49 -92.723 -92.743 -93.35 -93.36
4 101.702 0.03 0.03 0.03 -93.47 -93.50 -92.723 -92.753 -93.34 -93.37
5 101.703 0.05 0.05 0.06 -93.47 -93.52 -92.723 -92.773 -93.32 -93.38
6 101.704 0.06 0.06 0.08 -93.47 -93.53 -92.723 -92.783 -93.33 -93.40

CodePudding user response:

You could put them in the order you want in scale_colour_manual.

Using pivot_longer you could also just have the one geom_*.

library(tidyverse)

tribble(
  ~kp, ~var1, ~var2, ~var3, ~var4, ~var5, ~var6, ~var7, ~var8, ~var9,
  101.699, 0.01, 0.01, 0.00, -93.46, -93.47, -92.713, -92.723, -93.33, -93.33,
  101.700, 0.01, 0.01, 0.01, -93.47, -93.48, -92.723, -92.733, -93.34, -93.35,
  101.701, 0.02, 0.02, 0.01, -93.47, -93.49, -92.723, -92.743, -93.35, -93.36,
  101.702, 0.03, 0.03, 0.03, -93.47, -93.50, -92.723, -92.753, -93.34, -93.37,
  101.703, 0.05, 0.05, 0.06, -93.47, -93.52, -92.723, -92.773, -93.32, -93.38,
  101.704, 0.06, 0.06, 0.08, -93.47, -93.53, -92.723, -92.783, -93.33, -93.40
) |> 
  pivot_longer(starts_with("var")) |> 
  filter(!name %in% c("var1", "var2", "var3")) |> 
  ggplot(aes(kp, value, colour = name))   
  geom_line()  
  labs(y = "Depth (m)", x = "KP", title = "Track")   
  theme_bw()  
  scale_colour_manual(labels= c("var4", "var5", "var6", "var7", "var8","var9"),
                      values = c("red","green","red", "green", "blue","darkgrey")) 
  theme(axis.text.x=element_text(size=14), axis.title.x = element_text(size=16),
        axis.text.y=element_text(size=14), axis.title.y = element_text(size=16),
        plot.title = element_text(size=20, face="bold", color = "darkblue"))

Created on 2022-05-05 by the enter image description here

  • Related