← Back to R Course | Chapter 10: Data Visualization | Lesson 1 of 7

plot()

plot draws a picture of your data, like dots on graph paper. Give it two lists of numbers and it shows how one relates to the other.

In this page:

  1. plot()
Syntax
r
plot(x, y, type = "p", main = "title", xlab = "x label", ylab = "y label")

plot()

plot(x, y) is R's general-purpose base plotting function, drawing a scatter plot by default from two numeric vectors.

Judge0 (and other non-interactive environments) capture plot output as a saved image rather than displaying a window. plot() accepts many arguments (type, col, pch, etc.) to customize its appearance.

Note:
  • type = "l" draws a line plot instead of scattered points.
  • type = "b" draws both points and connecting lines.

Example: plot()

markup
x <- 1:10
y <- x^2

# In a graphical R environment this renders a plot window / image file.
# Here we just confirm the data plot() would use is correct:
cat("x values:", x, "\n")
cat("y values:", y, "\n")
cat("plot(x, y) would draw a scatter plot of y = x^2\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing x and y of different lengths, which gives an error like x and y lengths differ.
  2. Running plot() in a script and expecting a window with Rscript, when the plot goes to a file named Rplots.pdf unless a device is opened.
  3. Calling plot() again and expecting to add to the existing plot, when it starts a new one; use points() or lines().
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.