ncibtep@nih.gov

Bioinformatics Training and Education Program

BTEP Question Forum

BTEP maintains several Question and Answer Forums of interest to the NCI/CCR community.
Currently, there are forums on these topics listed below:

If you wish to ask a question go to the Ask Question Page and submit your question.

 Back to Questions

Labeling select data points on a plot

If I want to label one data point in a PCA or volcano plot, how would I go about doing that?

1

1 Answer:


There multiple ways to label individual points in a plot. Here is one such example using the data set `iris`.
 
The basic steps are:
Step 1 - filter data frame with points we want to label
Step 2 - add labels using geom_text and call filtered data set
 
`ggrepel` is a great package to use if you want to avoid labels overlapping. 
 
Example Code:
 

#run pca
pca <- prcomp(iris[,1:4], scale = TRUE)

#Build a data frame to plot
pcaData <- as.data.frame(pca$x[, 1:2]) # extract first two PCs
pcaData <- cbind(pcaData, iris) # add data
pcaData <- rownames_to_column(pcaData,"ID") #add ID column

#create a filtered data frame with points we want to label
fdata<-pcaData %>%
filter(ID %in% c(1,5,10))

#Plot
ggplot(pcaData) +
aes(PC1, PC2, color = Species, shape = Species) + # define plot area
geom_point(size = 2) + # adding data points
coord_fixed() + # fixing coordinates
geom_text(data=fdata,
aes(x = PC1, y = PC2,label=ID),color="black") # label points

 


Answered on April 4th, 2023 by