Commit 2763e7b3 authored by Chaos's avatar Chaos
Browse files

extend the volcano figure

parent 18a6615e
Loading
Loading
Loading
Loading
+93 −37
Original line number Diff line number Diff line
@@ -158,17 +158,18 @@ de.gene <- function(
	}
}


volcano_plot <- function(
					.data,
					top_gene_number = 10,
					fold_change = 1.5,
					p_value = 0.05,
					special.gene = NULL,
					special.label = "special",
					show_p = TRUE,
					show_fc = TRUE,
					show_gene_number = TRUE,
					show_legend = FALSE,
					legend_position = "right",
					sub_gene = NULL,
					sub_id = NULL,
					color_up = "#E64B35",
@@ -180,7 +181,8 @@ volcano_plot <- function(
					fc_line_type = "dashed",
					p_line_color = "black",
					left_fc_line_color = "black",
					right_fc_line_color = "black"
					right_fc_line_color = "black",
					font = "serif"
){
	if(is.null(sub_gene)){sub_gene <- .data$gene_name}
	if(is.null(sub_id)){sub_id <- .data$gene_id}
@@ -244,43 +246,49 @@ volcano_plot <- function(
		
		dt[
			label_special %in% special.gene, 
			color := "special"
			color := special.label
		]
	}

	if(is.null(special.gene)){
		label_all <- c("Down","Not significant","Up")
		color_all <- c(color_down,color_ns,color_up)
	}else{
		label_all <- c("Down","Not significant",special.label,"Up")
		color_all <- c(color_down,color_ns,color_special,color_up)
	}

	plot <-	ggplot() + 
			geom_point(
				data = dt[color == "not_sig"],
				aes(log2FC,log10P),
				color = color_ns,
				aes(log2FC,log10P,color = color),
				size = 0.8,
				show.legend = show_legend
			) +
			geom_point(
				data = dt[color == "up"],
				aes(log2FC,log10P),
				color = color_up,
				aes(log2FC,log10P,color = color),
				size = 0.8,
				show.legend = show_legend
			) +
			geom_point(
					data = dt[color == "down"],
					aes(log2FC,log10P),
					color = color_down,
					aes(log2FC,log10P,color = color),
					size = 0.8,
					show.legend = show_legend
			) +
			geom_point(
					data = dt[color == "special"],
					aes(log2FC,log10P),
					color = color_special,
					data = dt[color == special.label],
					aes(log2FC,log10P,color = color),
					size = 0.8,
					show.legend = show_legend
			)
			) +
			scale_color_manual(values = color_all, label = label_all)

	if(isFALSE(hyperbola)){
		xlimits <- max(abs(min(dt$log2FC)),abs(max(dt$log2FC)))
	extend_size <- 3
	xlimits <- max(abs(min(dt$log2FC)),abs(max(dt$log2FC))) + extend_size

	if(isFALSE(hyperbola)){
		plot <-	plot +
				geom_hline(
					yintercept = -log10(p_value),
@@ -302,7 +310,17 @@ volcano_plot <- function(
			hyperbola_dt_l <-	dt[log2FC < 0,.(log2FC = log2FC * -1, hyperbola_log10P)]
		}

		hyperbola_dt <- rbind(hyperbola_dt_l,hyperbola_dt_r)[hyperbola_log10P > -log10(p_value)]
		hyperbola_extend <-	data.table(
								log2FC = c((xlimits * -1):(xlimits * -1 + extend_size),(xlimits - extend_size):xlimits)
							)[
								,hyperbola_log10P := 1/((abs(log2FC)) - log2(fold_change)) - log10(p_value)
							]

		hyperbola_dt <- rbind(
							hyperbola_extend,
							hyperbola_dt_l,
							hyperbola_dt_r
						)[hyperbola_log10P > -log10(p_value)]

		plot <-	plot +
				geom_line(
@@ -320,46 +338,81 @@ volcano_plot <- function(
				data = dt,
				aes(x = log2FC, y = log10P),
				label = dt$label_up,
				max.overlaps = Inf,
				box.padding = 1,
				box.padding = 0.1,
				na.rm = T,
				color = color_up
				color = color_up,
				min.segment.length = 0,
				force = 2,
				force_pull = 2,
				max.overlaps = Inf,
				segment.linetype = 3, #线段类型,1为实线,2-6为不同类型虚线
				segment.color = 'black', #线段颜色
				segment.alpha = 0.5, #线段不透明度
				nudge_x = xlimits - dt$log2FC, #标签x轴起始位置调整
				direction = "y", #按y轴调整标签位置方向,若想水平对齐则为x
				hjust = 0,
				fontface = "italic"
			) +
			geom_text_repel(
				data = dt,
				aes(x = log2FC, y = log10P),
				label = dt$label_down,
				box.padding = 0.1,
				max.overlaps = Inf,
				box.padding = 1,
				na.rm = T,
				color = color_down
				color = color_down,
				min.segment.length = 0,
				force = 2,
				force_pull = 2,
				segment.linetype = 3, #线段类型,1为实线,2-6为不同类型虚线
				segment.color = 'black', #线段颜色
				segment.alpha = 0.5, #线段不透明度
				nudge_x = xlimits * -1 - dt$log2FC, #标签x轴起始位置调整
				direction = "y", #按y轴调整标签位置方向,若想水平对齐则为x
				hjust = 0,
				fontface = "italic"
			) +
			geom_text_repel(
				data = dt,
				aes(x = log2FC, y = log10P),
				label = dt$label_special,
				max.overlaps = Inf,
				box.padding = 1,
				box.padding = 0.1,
				na.rm = T,
				color = color_special
				color = color_special,
				min.segment.length = 0,
				force = 2,
				force_pull = 2,
				max.overlaps = Inf,
				segment.linetype = 3, #线段类型,1为实线,2-6为不同类型虚线
				segment.color = 'black', #线段颜色
				segment.alpha = 0.5, #线段不透明度
				nudge_x = xlimits - dt$log2FC, #标签x轴起始位置调整
				direction = "y", #按y轴调整标签位置方向,若想水平对齐则为x
				hjust = 0,
				fontface = "italic"
			) +
			labs(
				x = expression(log[2]("Fold Change")),
				y = expression(-log[10](adj.p-Value))
				y = expression(-log[10]("adj.p.Value"))
			) + 
			guides(color = "none") + 
			theme(plot.margin = unit(rep(1,4),'lines'))
			theme(
				plot.margin = unit(rep(1,4),'lines'),
				legend.position = legend_position,
				legend.title = element_blank(),
				text = element_text(family = font)
			)

	if(isTRUE(show_p))
	{
		plot <-	plot +
				annotate(
					geom = "text",
					x = min(dt$log2FC),
					x = xlimits * -1,
					y = -log10(p_value),
					hjust = 0,
					vjust = 1,
					label = paste0("p-value = ",p_value)
					label = paste0("adj.p.Value < ",p_value),
					family = font
				) 
	}

@@ -367,10 +420,11 @@ volcano_plot <- function(
		plot <-	plot +
				annotate(
					geom = "text",
					x = min(dt$log2FC),
					x = xlimits * -1,
					y = -log10(p_value) - 0.3,
					hjust = 0,
					label = paste0("Fold Change = ",fold_change)
					label = paste0("Fold Change > ",fold_change),
					family = font
				)
	}

@@ -379,15 +433,17 @@ volcano_plot <- function(
		plot <-	plot +
				annotate(
					"text",
					x = c(-log2(fold_change),min(dt$log2FC)) |> mean(), 
					y = max(dt$log10P),
					label = stat[regulation == "down",label]
					x = xlimits/2, 
					y = max(dt$log10P) + 0.5,
					label = stat[regulation == "down",label],
					family = font
				) +
				annotate(
					"text",
					x = c(log2(fold_change),max(dt$log2FC)) |> mean(), 
					y = max(dt$log10P),
					label = stat[regulation == "up",label]
					x = xlimits/2 * -1, 
					y = max(dt$log10P) + 0.5,
					label = stat[regulation == "up",label],
					family = font
				)
	}