R Tutorial Series: Labeling Data Points on a Plot

[This article was first published on R Tutorial Series, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

There are times that labeling a plot’s data points can be very useful, such as when conveying information in certain visuals or looking for patterns in our data. Fortunately, labeling the individual data points on a plot is a relatively simple process in R. In this tutorial, we will use the Calibrate package’s textxy function to label the points on a scatterplot.

Tutorial Files

Before we begin, you may want to download the sample data (.csv) used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains information used to estimate undergraduate enrollment at the University of New Mexico (Office of Institutional Research, 1990). Note that this tutorial assumes that this data has already been read into R and saved into a variable named enrollmentData.


To begin, we need to create a scatterplot using the plot(x,y) function. With our example data, we will plot the year on the x axis and the unemployment rate on the y axis.
  1. > #generate a plot using the plot(x,y) function
  2. > #plot year on the x axis and unemployment rate on the y axis
  3. > plot(enrollmentData$YEAR, enrollmentData$UNEM)

For a more detailed description of plotting data in R, see the article on scatterplots.


Within the calibrate package, the textxy() function can be used to label a plot’s data points. The textxy() function accepts the following arugments (“Label points in a plot,” n.d.).
  • x: the x values of the plot’s points
  • y: the y values of the plot’s points
  • labs: the labels to be associated with the plot’s points
    • Optional
    • cx: used to resize the label font
    • dcol: used to set the label color; defaults to black
    • m: sets the origin of the plot; defaults to (0,0)
    Here, we will use textxy() to add labels for the enrollment at the University of New Mexico to each of our plot’s data points.
    1. > #if necessary, install the calibrate package
    2. > #install.packages(“calibrate”)
    3. > #load the calibrate package
    4. > library(calibrate)
    5. > #use the textxy() function to add labels to the preexisting plot’s points
    6. > #add labels for the total enrollment
    7. > textxy(enrollmentData$YEAR, enrollmentData$UNEM, enrollmentData$ROLL)

    In this case, adding labels to our data points helps us to better assess the relationships in our dataset.

    Complete Data Point Labeling Example

    To see a complete example of how a plot’s data points can be labeled in R, please download the Data Point Labeling (.txt) file.


    Label points in a plot. (n.d.). Retrieved September 19, 2010 from http://rss.acs.unt.edu/Rdoc/library/calibrate/html/textxy.html
    Office of Institutional Research (1990). Enrollment Forecast [Data File]. Retrieved November 22, 2009 from http://lib.stat.cmu.edu/DASL/Datafiles/enrolldat.html

    To leave a comment for the author, please follow the link and comment on their blog: R Tutorial Series.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

    Never miss an update!
    Subscribe to R-bloggers to receive
    e-mails with the latest R posts.
    (You will not see this message again.)

    Click here to close (This popup will not appear again)