R plot() Graph Types Explained

R 语言实例R Language Examples

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 valueDescriptionApplicable Scenarios
"p"Points (default)Scatter plot
"l"LineLine chart
"b"Points and linesShows data point positions and trends
"o"Overlaid points and linesTime series
"h"Vertical linesSpectrum chart
"s"Step linesStep data
"n"No plottingOnly 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)

Running the above code will display a temperature change chart overlaid with vertical lines and connecting lines.

R 语言实例R Language Examples

Other Extensions