Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I shade plot subregion and use ggrepel to label a subset of data points?

I made this Volcano plot and am hoping to improve it as follows:

made this Volcano plot

  1. fully shade the region with blue data points: with my current code, I wasn't able to extend the shade beyond what you see. I would like it to go all the way to the plot area limits.

  2. geom_text allowed me to label a subset of data points, but doing it with ggrepel should add lines connecting the data points with labels thus improving labeling clarity. How can I reuse the existing geom_text code in ggrepel to achieve this?

Here is my code:

ggplot(vol.new, aes(x = log2.fold.change, y = X.NAME., fill = Color)) + # Define data frame to be used for plotting; define data for x and y axes; crate a scatterplot object.

  geom_point(size = 2, shape = 21, colour = "black") + # Define data point style.

  ggtitle(main.title, subtitle = "Just a little subtitle") + # Define title and subtitle.

  labs(x = x.lab, y = y.lab) + # Define labels for x and y axes.

  scale_x_continuous(limits = c(-3, 3), breaks = seq(-3, 3, by = 0.5)) + # Define x limits, add ticks.
  scale_y_continuous(limits = c(0, 6), breaks = seq(0, 6, by = 1)) + # Define y limits, add ticks.

  theme(
    plot.title = element_text(family = "Arial", size = 11, hjust = 0), # Title size and font.
    plot.subtitle = element_text(family = "Arial", size = 11), # Subtitle size and font.
    axis.text = element_text(family = "Arial", size = 10), # Size and font of x and y values.
    axis.title = element_text(family = "Arial", size = 10), # Size and font of x and y axes.
    panel.border = element_rect(colour = "black", fill = NA, size = 1), # Black border around the plot area.
    axis.ticks = element_line(colour = "black", size = 1), # Style of x and y ticks.
    legend.position = "none"
  ) + # Remove legend.

  geom_hline(yintercept = 1.30103, colour = "black", linetype = "dashed", size = 0.75) + # Horizontal significance cut-off line.
  geom_vline(xintercept = 0.584963, colour = "black", linetype = "dashed", size = 0.75) + # Vertical significance cut-off line (+).
  # geom_vline (xintercept = -0.584963, colour = "black", linetype = "dashed", size = 0.75) #Vertical significance cut-off line (-)

  scale_fill_manual(breaks = c("blue", "red"), values = c("deepskyblue3", "firebrick1")) + # Costum colors of data points based on "PursFur" column.

  geom_text(aes(label = ifelse(PursFur == 1, as.character(Protein.ID), "")), hjust = 0, vjust = -0.25) + # Add identifiers to a subset of data points.

  annotate("text", x = 2.9, y = 1.45, label = "P = 0.05", size = 4, fontface = "bold") + # Label to horizontal cut-off line.
  annotate("text", x = 0.68, y = 5.9, label = "1.5-fold", size = 4, fontface = "bold", srt = 90) + # Label to vertical cut-off line.
  annotate("rect", xmin = 0.584963, xmax = 3, ymin = 1.30103, ymax = 6, alpha = .2) # Shade plot subregion.
like image 776
JernejT Avatar asked Nov 13 '18 01:11

JernejT


People also ask

How do I label individual points in R?

To add labels to scatterplot points in base R you can use the text() function, which uses the following syntax: text(x, y, labels, …)

What is Ggrepel?

ggrepel implements functions to repel overlapping text labels away from each other and away from the data points that they label. The algorithm works as follows: For each box: Move the box into the allowed plotting area.

What is Geom_text_repel?

Description. geom_text_repel adds text directly to the plot. geom_label_repel draws a rectangle underneath the text, making it easier to read. The text labels repel away from each other and away from the data points.


1 Answers

As suggested in the comments by @hrbrmstr and @zx8754, here are the modifications I made to the code above.

To solve the shading problem (via @hrbrmstr):

annotate ("rect", xmin = 0.584963, xmax = Inf, ymin = 1.30103, ymax = Inf, alpha = .2)

To solve the labeling question (via @zx8754):

geom_label_repel (aes (label = ifelse (PursFur == 1, as.character (Protein.ID), '')), nudge_x = 1.3, direction = "x")

And here is the outcome after these two changes:

enter image description here

See this website and this nice ggrepel tutorial to dive further into the second part of my initial question.

like image 106
JernejT Avatar answered Oct 05 '22 08:10

JernejT