0

I have a data like below and I want to create a plot for every column vs the very first column like in the code. However, how is it possible to give different colours to points based on row numbers or x axis test? For example all geom_points of NUM_{1:3}_A=green color, NUM_{1:3}_B=blue, NUM_{1:3}_C=red color?

Sample  GENOME_SIZE BAIT_TERRITORY  TARGET_TERRITORY    BAIT_DESIGN_EFFICIENCY  TOTAL_READS PF_READS    PF_UNIQUE_READS
NUM_1_A 3137161264  42836424    35735556    0,834233    49935394    49935394    49935394
NUM_2_A 3137161264  42836424    35735556    0,834233    40161571    40161571    40161571
NUM_3_A 3137161264  42836424    35735556    0,834233    51389125    51389125    51389125
NUM_1_B 3137161264  42515446    34349619    0,807933    74447359    74447359    74447359
NUM_2_B 3137161264  42515446    34349619    0,807933    67243634    67243634    67243634
NUM_3_B 3137161264  42515446    34349619    0,807933    73536958    73536958    73536958
NUM_1_C 3137161264  44215022    37291688    0,843417    72333296    72333296    72333296
NUM_2_C 3137161264  44215022    37291688    0,843417    74310969    74310969    74310969
NUM_3_C 3137161264  44215022    37291688    0,843417    85559698    85559698    85559698

My script:

dat <- read.table("file.txt", sep = "\t", h = T, stringsAsFactors = F)
col_names <- colnames(dat)
col_names <- col_names[-1]
plot_list <- list()
pdf("testUSgraph.pdf")
for (i in col_names){
  plot <- ggplot(dat, aes_string(x=as.factor(dat$Sample), y=i)) +
    geom_point(size = 1) +
    labs(y = i,
         x = "some type",
         colour = "") +
    scale_color_manual(name = "type",
                       values = c("dat$Sample[1:3]" = "green",
                                  "dat$Sample[4:6]" = "blue",
                                  "dat$Sample[7:9]" = "red"),
                       labels = c("A", "B", "C")) +
    ggtitle(i) +
    theme_bw() +
    #theme(axis.text.x=element_text(vjust = 0.5,angle=90))
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text.x=element_text(size=rel(1), angle=90))
  plot_list[[i]] <- plot
}
print(plot_list)
dev.off()
user3224522
  • 1,119
  • 8
  • 19

1 Answers1

1

You can add a grp column to use as colour aesthetic.

Minimal example:

data %>%
    mutate(grp = str_remove(Sample, "^.+_\\d+_")) %>%
    ggplot(aes(Sample, PF_READS, colour = grp)) + 
    geom_point()

enter image description here

More importantly, don't use $-indexing inside aes. This is a recipe for some very bad downstream side effects: see GH issue and SO post.


Sample data

data <-read.table(text = "Sample  GENOME_SIZE BAIT_TERRITORY  TARGET_TERRITORY    BAIT_DESIGN_EFFICIENCY  TOTAL_READS PF_READS    PF_UNIQUE_READS
NUM_1_A 3137161264  42836424    35735556    0,834233    49935394    49935394    49935394
NUM_2_A 3137161264  42836424    35735556    0,834233    40161571    40161571    40161571
NUM_3_A 3137161264  42836424    35735556    0,834233    51389125    51389125    51389125
NUM_1_B 3137161264  42515446    34349619    0,807933    74447359    74447359    74447359
NUM_2_B 3137161264  42515446    34349619    0,807933    67243634    67243634    67243634
NUM_3_B 3137161264  42515446    34349619    0,807933    73536958    73536958    73536958
NUM_1_C 3137161264  44215022    37291688    0,843417    72333296    72333296    72333296
NUM_2_C 3137161264  44215022    37291688    0,843417    74310969    74310969    74310969
NUM_3_C 3137161264  44215022    37291688    0,843417    85559698    85559698    85559698", header = T)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68