1

Summary:

GGplot seems to plot lineplots in an alphabetical order when aes(group = ID) is set. The line for each ID is plotted according to its (alphabetical) order. In my case this results in a quite busy plot. I'd like to have the lines of a specific group in the background. More specifically I want those lines in the background which have the most values in their group, so that they are not overlapping the lines from groups with only a few values.

Question:

How can I reorder the data in that way that the group with the most values is plotted first?

Code:

First some code to generate data to work with (not pretty but the result is good):

rm(list=ls()) 
set.seed(42)
library('ggplot2')

numOfValues <- c(20, 6, 3, 2)
System <- c(letters[1:4])
times <- c(1,2,3)
slope <- sample(1:4, size = 4)

df <- data.frame()
row <- 1
for (sys in 1:length(System)) {
  for (num in 1:numOfValues[sys]) {
    for (t in 1:length(times)) {
      # this seems stupid, but to be consistent with my data I need unique but
      # ordered ID's
      df[row, 'ID'] <- paste('P', 
                             num + if (System[sys] == 'a') {0} 
                             else if (System[sys] == 'b') {20} 
                             else if (System[sys] == 'c') {26}
                             else if (System[sys] == 'd') {28} , 
                             sep='_') 
      
      df[row, 'System'] <- System[sys]
      df[row, 'Time'] <- paste('T', times[t], sep = '')
      df[row, 'Value'] <- runif(1, 1, 10) + times[t] + slope[sys]
      row <- row + 1
    }
  }
}

This is my plotting code with ggplot

p <- ggplot(data = df,
            aes(x = Time,
            y = Value, 
            group = ID,
            colour = System,
            label = ID)) +
  geom_line(size = 1.5) +
  geom_point(size = 3,
             aes(shape = System)) +
  theme_bw()

p

This is resulting in this graph:

unordered plot

You can see that some red lines from group a a on top and some are more in the background behind purple/green lines. It seems like that the lines get plotted in the order of the group aesthetic from ggplot, in this case by ID. This is mentioned here:

How can I define line plotting order in ggplot2 for grouped lines?.

As suggested there, I could plot each group in a separate geom_line call but I think there must be an other way. The second solution posted in the above mentioned question got me thinking, but I'm not able to adopt it to my code, since I need to group the (sample) ID's by my Column 'System'.

I thought if I refactor my ID column in the correct order like my groups I can tell ggplot to plot those lines first which correspond to the group with the most values inside.

What else could I try to tell ggplot to plot the lines not by ID but by System? How can I order the factored ID's according to their group?

1 Answer 1

0

I found the solution in the process of writing the question, and I thought I might share this, since it could be helpfull for other people :°).

It seems like we can refactor the leves according to a different column like this:

df$ID <- as.factor(df$ID)
df$System <- as.factor(df$System)

df$ID <- factor(df$ID, 
               levels = c(c(unique(as.factor(df$ID[c(which(df$System == 'a'))]))),
                          c(unique(as.factor(df$ID[c(which(df$System == 'b'))]))),
                          c(unique(as.factor(df$ID[c(which(df$System == 'c'))]))),
                          c(unique(as.factor(df$ID[c(which(df$System == 'd'))])))
                          )
               )

Executing this, we're looking for those row'swhich are part of group a. Then we take the line number to get the ID's of those samples and pass them as an unique (there are multiple samples with the same name) vector to the levels parameter. We repeat that for each group.

This results in this plot (take the code from the question):

Ordered Plot

Now the lines in the largest group a are plotted first

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.