Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.4 kB
1
Indexable
Never
library(ggplot2)
library(scales)

# Sample data
df <- data.frame(names = c('MEP', 'HSC', 'Cycling', 'GMP'),
                 NES = c(1.789621, -1.750293, -1.274638, -1.460457),
                 number = c(230, 225, 2205, 219),
                 p_value = c(0.000001, 0.00001, 0.00004, 0.00270))



df$p_value=-log10(df$p_value)
limit=c(-max(df$p_value),max(df$p_value))
# Adjust p-value for negative NES
df$p_value <- ifelse(df$NES < 0, -df$p_value, df$p_value)

# Create categorical variable for the number column
df$category <- cut(df$number,
                   breaks = c( 0, 200, 500, 2000,  Inf),
                   labels = c("0-200", "200-500", "500-2000", ">2000"),
                   include.lowest = TRUE)


# Define color gradient for p-value
color_gradient <- c("red", "pink" ,"white", "yellow","green")

# Create the plot
p <- ggplot(df, aes(x = NES, y = reorder(names, NES))) +
  geom_segment(aes(xend = 0, yend = reorder(names, NES)), size = 1, color = "black") +
  geom_point(aes(size = category, fill = p_value), shape = 21) +  # Use p_value for fill
  scale_fill_gradientn(colors = color_gradient, limits = limit, name = "p-value Intensity") +
  scale_size_manual(values = c("0-200" = 3, "200-500" = 5, "500-2000" = 7,  ">2000" = 9), name = "Number Category") +
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_vline(xintercept = c(-2, -1, 1, 2), linetype = "solid", color = "grey", alpha = 0.5) +
  theme_minimal() +
  labs(x = "NES", y = "", title = "GSEA") +
  theme(
    axis.text.y = element_text(color = "black"),
    axis.ticks.y = element_blank(),
    legend.position = "right",
    plot.title = element_text(hjust = 0.5),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    legend.text = element_text(size = 12),  # Adjust legend text size
    legend.box.margin = margin(10, 10, 10, 10),  # Add margin around legend box
    #legend.key = element_rect(color = "black", size = 1)  # Box around legend key
  ) +
  guides(size = guide_legend(order = 1), fill = guide_colorbar(order = 2)) +  # Control legend order

  # Custom X axis delimiter
  annotate("text", x = 0, y = -0.5, label = "|", size = 10, color = "grey", vjust = 0, hjust = 0.5) +

  # Move the legend outside the plot
  theme(
    legend.margin = margin(0, 0, 0, 10)  # Margin around legend
  )

# Print the plot
print(p)
Leave a Comment