6. Plots - JulTob/R GitHub Wiki
🗺️ Map of Basic Plots
“A single chart can reveal more than a hundred raw numbers. Draw wisely, sailor.”
R comes armed with simple yet powerful plotting tools, perfect for exploring yer data quickly before divin’ into more advanced visualizations.
Here’s how to summon the three most trusted maps of the data sea.
⸻
📦 Histogram (hist())
Great for viewin’ the distribution of numeric data: how many times values appear, grouped into bins.
> datos <- c(1,2,2,3,3,3,3,3,3,4,4,5,5,6,6,7,2,1)
> hist(datos)
This draws a histogram showing how often each value range appears.
Customizations
> hist(datos, breaks = 5, col = "skyblue", main = "Histogram of datos", xlab = "Value")
- breaks = 5 sets number of bins
- col adds color
- main and xlab set the title and x-axis label
⸻
🌱 Stem-and-Leaf Plot (stem())
This be a classic explorer’s tool for small datasets, like a numeric map printed sideways!
> stem(datos)
Ye might see:
The decimal point is at the |
0 | 00
2 | 000000000
4 | 0000
6 | 000
Each line shows a group of numbers, easy to read when the sea is calm and the data’s not too wild.
⸻
📦 Boxplot (boxplot())
Use this to summarize a numeric vector: shows median, quartiles, outliers all in one tidy box.
> boxplot(datos, col = 2)
- col = 2 adds color (color 2 is red)
- The line in the box is the median
- The box edges are the 1st and 3rd quartiles
- The whiskers show the range, with dots for outliers ⸻
You can also use named colors
> boxplot(datos, col = "tomato")
⸻
Multiple Boxplots
> group <- c("A", "A", "A", "B", "B", "B", "B", "B", "A", "A", "B", "B", "A", "A", "B", "A", "A", "B")
> boxplot(datos ~ group, col = c("gold", "lightblue"))
This makes grouped boxplots for comparing distributions by category.
⸻
⚓ The Plotting Basics
Use plot() for general plots—scatterplots, line graphs, and more. Its behavior changes based on the input.
> x <- 1:10
> y <- x^2
> plot(x, y)
This draws a simple scatterplot: x on the horizontal axis, y on the vertical.
⸻
📈 Plot Types (type=)
Control what kind of chart ye draw:
> plot(x, y, type = "l") # "l" for lines
> plot(x, y, type = "b") # "b" for both points and lines
> plot(x, y, type = "o") # "o" for overplotted points and lines
⸻
🎨 Titles, Labels, Colors
Customize your chart:
> plot(x, y,
main = "Quadratic Curve",
xlab = "X-axis",
ylab = "Y-axis",
col = "blue",
pch = 19)
- main = title
- xlab, ylab = axis labels
- col = color
- pch = point style (19 = solid circle)
🧱 Barplot
For categorical data:
> counts <- table(c("Heads", "Tails", "Heads", "Heads"))
> barplot(counts, col = "steelblue", main = "Coin Flips")
⸻
🌪️ Line Plots & Time Series
> time <- 1:12
> temp <- c(14, 15, 16, 18, 21, 24, 27, 26, 23, 19, 16, 14)
> plot(time, temp, type = "l", col = "darkgreen", main = "Monthly Temps")
⸻
🔍 Extras: Grid, Legend, Axis Tweakery
> grid()
> legend("topleft", legend = "Temps", col = "darkgreen", lty = 1)
> axis(1, at = 1:12, labels = month.abb)
⸻
📦 Saving Plots
> png("my_plot.png")
> plot(x, y)
> dev.off()
You can also use pdf(), jpeg(), or export from RStudio’s Plots pane.
⸻
🧭 Summary of Core Plot Types
| Function | Use Case |
|---|---|
| plot() | Versatile base plotting |
| hist() | Numeric distribution |
| boxplot() | Summary with outliers |
| stem() | Print-friendly distribution |
| barplot() | Categorical data |
| lines() / points() | Add to existing plots |