Based Plot: Scatter Plot
๐ Scatter Plot Cheat Sheet (R Base)
Command | Purpose |
---|---|
plot(x, y) |
Basic scatter plot of two variables |
main = "Title" |
Add main title |
xlab = "X-axis" , ylab = "Y-axis" |
Axis labels |
pch |
Point shape (0 = square, 1 = circle, 2 = triangle, โฆ) |
col |
Point border color |
bg |
Point fill color (only for pch = 21โ25) |
cex |
Point size (default = 1) |
legend(position, legend, col, pch, title) |
Add legend to explain groups |
text(x, y, labels, pos, col) |
Annotate points with text |
abline(lm(y ~ x, data=df), col) |
Add linear regression line |
lines(lowess(x, y), col) |
Add smooth (non-linear) line |
data(iris) , data(cars) , data(gapminder) |
Example datasets for plotting |
dplyr::mutate() with if_else() or case_when() |
Conditional coloring or symbols |
RColorBrewer::display.brewer.all() |
Show all color palettes |
brewer.pal(n, "Accent") |
Extract n colors from palette |
par(bg="lightblue") |
Change background color |
rect(..., col="gray") |
Fill plot box background |
grid(lty=1, col="white") |
Add grid lines |
Quick Tips:
Use
pch
andcol
to differentiate groups.Combine
abline()
andlines(lowess())
to show both linear and non-linear fits.Use
RColorBrewer
for professional, colorblind-friendly palettes.Control background and grid with
par()
,rect()
, andgrid()
.