R plot() Graph Types Explained
The R plot() function can create multiple types of graphs through the type parameter.
Different type options are suitable for different data visualization needs.
Common values for the type parameter of plot():
| type value | Description | Applicable Scenarios |
|---|---|---|
| "p" | Points (default) | Scatter plot |
| "l" | Line | Line chart |
| "b" | Points and lines | Shows data point positions and trends |
| "o" | Overlaid points and lines | Time series |
| "h" | Vertical lines | Spectrum chart |
| "s" | Step lines | Step data |
| "n" | No plotting | Only sets the coordinate system |
Example
# Generate a month of temperature data
temp <- c(5, 7, 10, 14, 18, 22, 25, 28, 26, 20, 15, 10,
8, 6, 9, 13, 17, 21, 24, 27, 25, 19, 14, 9,
7, 5, 8, 12, 16, 20)
days <- 1:30
# Overlay multiple types on the same plot
plot(days, temp, type = "n", # First set up the coordinate system without plotting
main = "Temperature changes this month",
xlab = "Date", ylab = "Temperature (°C)",
ylim = c(0, 30))
# Add vertical lines (type = "h")
lines(days, temp, type = "h",
col = "lightblue", lwd = 2)
# Add points and connecting lines
lines(days, temp, type = "o",
col = "coral", lwd = 2, pch = 19, cex = 0.6)
temp <- c(5, 7, 10, 14, 18, 22, 25, 28, 26, 20, 15, 10,
8, 6, 9, 13, 17, 21, 24, 27, 25, 19, 14, 9,
7, 5, 8, 12, 16, 20)
days <- 1:30
# Overlay multiple types on the same plot
plot(days, temp, type = "n", # First set up the coordinate system without plotting
main = "Temperature changes this month",
xlab = "Date", ylab = "Temperature (°C)",
ylim = c(0, 30))
# Add vertical lines (type = "h")
lines(days, temp, type = "h",
col = "lightblue", lwd = 2)
# Add points and connecting lines
lines(days, temp, type = "o",
col = "coral", lwd = 2, pch = 19, cex = 0.6)
Running the above code will display a temperature change chart overlaid with vertical lines and connecting lines.
Other Extensions
R Language Examples