Home > OS >  How change color after certain intervals in ggplot2?
How change color after certain intervals in ggplot2?

Time:11-18

I have following data:

dput(data)

structure(list(category = c("GO:BP", "GO:BP", "GO:BP", "GO:BP", 
"GO:BP", "GO:BP", "GO:BP", "GO:BP", "GO:BP", "GO:BP", "GO:BP", 
"GO:BP", "GO:BP", "GO:BP", "GO:BP", "GO:BP", "Pathway", "GO:BP", 
"GO:BP", "GO:BP", "GO:BP", "GO:BP", "GO:BP"), ID = c("cell adhesion", 
"inflammatory response", "regulation of cell migration", "T cell activation", 
"innate immune response", "adaptive immune response", "angiogenesis", 
"cytokine-mediated signaling pathway", "wound healing", "regulation of ERK1 and ERK2 cascade", 
"extracellular matrix organization", "CD4-positive, alpha-beta T cell activation", 
"receptor signaling pathway via JAK-STAT", "natural killer cell activation", 
"mast cell activation", "CD8-positive, alpha-beta T cell activation", 
"Interleukin-4 and 13 signaling", "regulation of hormone levels", 
"response to hypoxia", "regulation of protein tyrosine kinase activity", 
"ERBB2-ERBB3 signaling pathway", "fatty acid derivative catabolic process", 
"cytolysis"), term = c("GO:0007155", "GO:0006954", "GO:0030334", 
"GO:0042110", "GO:0045087", "GO:0002250", "GO:0001525", "GO:0019221", 
"GO:0042060", "GO:0070372", "GO:0030198", "GO:0035710", "GO:0007259", 
"GO:0030101", "GO:0045576", "GO:0036037", "1470923", "GO:0010817", 
"GO:0001666", "GO:0061097", "GO:0038133", "GO:1901569", "GO:0019835"
), Genes = c("ERBB2,ACP5,IKZF1,GRHL2,SIRPG,NLGN4X,CCL4,CCL5,MMP2,CCL19,XCL1,ALOX15,FOXC2,SELE,SELL,NID2,SFRP2,ANK3,EMB,OLFM4,NTNG1,ST3GAL4,NLGN1,CYFIP2,TMEFF1,LY6D,RHOH,SKAP1,SOCS1,MYBPC1,ADAM19,CD244,AXL,LRRC32,TNFRSF14,IL20RB,NFASC,NCAM1,CD84,CCN4,AJAP1,ADAMDEC1,PCDH11X,SYK,NINJ2,GNRH1,LILRB4,LILRA1,LILRB3,P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L,PCSK1N,TRPA1,PCLO,CHST8,OSM,OXCT1,PCSK6,PAX6,NRG1,PDE3B,SLC30A8,ALDH8A1,CYP2S1,EXOC3L1,BMP8A,ABCC2,HSD17B1,HSD17B2,ERO1B,DUOX2,CPA3,CPE,MED1,NR1D1,CRABP1,RIMS2,RELN,PLB1,ABCA12,CHST9,ACE,KCNB1,DIO2,ACE2,LIF,HMGA2,LEF1", 
"ADM,NDRG1,SCN2A,AK4,MMP2,MMP3,VEGFD,ALDOC,MST1,B3GAT1,APLN,CA9,NPPC,SOCS3,NR4A2,TF,TH,CHRNA7,SERPINA1,PLAT,HSD11B2,HILPDA,VLDLR,NOX4,CST2,CST3,PRKCQ,CYBB,ACE,DDIT3,CXCR4,EGLN3,ANGPTL4,VHLL,LTA", 
"ERBB2,CCL5,LILRB4,SRCIN1,THY1,NRG1,VLDLR,NRG2,NOX4,RELN,ACE,PTPRC,EGFR,FBXW7", 
"ERBB2,NRG1,NRG2", "ACAT1,CYP4F11,OXCT1,ACOT7,HPGD,NUDT7", "GSDMB,C6,GZMB,CCL28,LBP,LYZ"
), pval = c(9.95e-15, 3.17e-06, 0.00126, 1.92e-11, 0.00055, 1.89e-05, 
0.00186, 6.06e-08, 0.00191, 0.000176, 0.000644, 5.6e-06, 0.00152, 
0.000378, 0.000796, 5.85e-05, 6.42e-05, 0.000359, 0.00163, 0.000192, 
0.000607, 0.000177, 0.000885), Zscore = c(4.528095586, 4.330522446, 
2.828427125, 5.820855001, 4.677476657, 5.598123172, 1.785687331, 
5.374011537, 2.91998558, 1.566698904, 2.556038602, 4.472135955, 
3.299831646, 3.356585567, 2.309401077, 2.529822128, 2.5, -0.145864991, 
-0.845154255, -0.534522484, -1.732050808, -0.816496581, -0.816496581
), Regulation = c("Activated", "Activated", "Activated", "Activated", 
"Activated", "Activated", "Activated", "Activated", "Activated", 
"Activated", "Activated", "Activated", "Activated", "Activated", 
"Activated", "Activated", "Activated", "Suppressed", "Suppressed", 
"Suppressed", "Suppressed", "Suppressed", "Suppressed"), Hit = c(137L, 
73L, 72L, 68L, 66L, 59L, 53L, 50L, 38L, 33L, 30L, 20L, 18L, 15L, 
12L, 10L, 16L, 47L, 35L, 14L, 3L, 6L, 6L), Count = c(1742L, 1048L, 
1249L, 704L, 1088L, 835L, 873L, 548L, 575L, 417L, 394L, 158L, 
204L, 136L, 102L, 55L, 114L, 695L, 512L, 114L, 5L, 22L, 29L), 
    GeneRatio = c(0.078645235, 0.069656489, 0.057646117, 0.096590909, 
    0.060661765, 0.070658683, 0.060710195, 0.091240876, 0.066086957, 
    0.079136691, 0.076142132, 0.126582278, 0.088235294, 0.110294118, 
    0.117647059, 0.181818182, 0.140350877, 0.067625899, 0.068359375, 
    0.122807018, 0.6, 0.272727273, 0.206896552), adj_pval = c(1.496e-11, 
    0.0002292, 0.02438, 7.227e-09, 0.0136, 0.0008949, 0.03311, 
    7.998e-06, 0.03386, 0.005822, 0.01509, 0.0003627, 0.02857, 
    0.01037, 0.01746, 0.002417, 0.01402, 0.00996, 0.03023, 0.006202, 
    0.01458, 0.005828, 0.01901)), class = "data.frame", row.names = c(NA, 
-23L))

I used following code to generate the plot:

p <- ggplot(data, aes(Zscore, ID))
p   geom_point(aes(colour=pval, size=Hit))  
    scale_color_gradientn(colours=rainbow(4), limits=c(0, 1))  
    geom_vline(xintercept=0, size=0.2, colour="blue", linetype="dotted")  
    theme(panel.background=element_rect(fill="gray95", colour="gray95"),
          panel.grid.major=element_line(size=0.1,linetype='solid', colour="gray90"), 
          panel.grid.minor=element_line(size=0.1,linetype='solid', colour="gray90"),
          axis.title.y=element_blank())  
    expand_limits(x=c(-2,3))  
    scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3))  
    scale_y_discrete(limits=rev(data$ID))

Output in the output, I want to change the color (to green) of positive Zscore values. Also to increase the hit to 0,10,20,25,50....

CodePudding user response:

The plot below is maybe not what is wanted.

  • To have the color change to green when the Z-score is positive means it should be mapped to Zscore, not to pval;
  • Also, the aesthetic size for lines was deprecated in ggplot2 3.4.0, I am using linewidth instead.
library(ggplot2)

p <- ggplot(data, aes(Zscore, ID))
p   geom_point(aes(colour = Zscore > 0, size = Hit))  
  scale_color_manual(
    name = 'Z-score',
    labels = c("Negative", "Positive"),
    values = c(`FALSE` = 'red', `TRUE` = 'green')
  )  
  scale_size_continuous(breaks = c(0,10,20,25,50, 75, 100))  
  geom_vline(xintercept=0, linewidth=0.2, colour="blue", linetype="dotted")  
  theme(panel.background=element_rect(fill="gray95", colour="gray95"),
        panel.grid.major=element_line(linewidth=0.1,linetype='solid', colour="gray90"), 
        panel.grid.minor=element_line(linewidth=0.1,linetype='solid', colour="gray90"),
        axis.title.y=element_blank())  
  expand_limits(x=c(-2,3))  
  # scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3, 4, 5, 6))  
  scale_x_continuous(name = 'Z-score', breaks = pretty(data$Zscore))  
  scale_y_discrete(limits=rev(data$ID))

Created on 2022-11-18 with reprex v2.0.2

  • Related