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:
Syntax
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()
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")
Login to try C/C++/Java/PHP code in the editor
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
- Passing
xandyof different lengths, which gives an error likex and y lengths differ. - Running
plot()in a script and expecting a window withRscript, when the plot goes to a file namedRplots.pdfunless a device is opened. - Calling
plot()again and expecting to add to the existing plot, when it starts a new one; usepoints()orlines().
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: