2023년 1월 21일 토요일

more examples of creating a basic line chart in R

 here are a few more examples of creating a basic line chart in R using different data and options:

  1. Using the ggplot2 package to create a line chart with different colors and linetypes for each group of data
# Load the ggplot2 package 
library(ggplot2) 
# Create a line chart with different colors and linetypes for each group of data 
ggplot(mtcars, aes(x = factor(cyl), y = mpg, color = factor(gear), linetype = factor(gear))) + geom_line() + xlab("Number of Cylinders") + ylab("Miles per Gallon") + ggtitle("Line Chart of Miles per Gallon by Number of Cylinders and Gears")
  1. Using the matplot() function with multiple lines() to create a multi-line chart
# Create a multi-line chart 
matplot(mtcars[, c("mpg", "wt", "hp")], type = "l", xlab = "Observation", ylab = "Value", main = "Multi-Line Chart of Miles per Gallon, Weight, and Horsepower") legend("topright", c("mpg", "wt", "hp"), lty = 1, col = 1:3)
  1. Using the ts() function to create a time series line chart
# Create a time series object 
ts_airpassengers <- ts(AirPassengers, start = c(1949, 1), frequency = 12) 
# Create a time series line chart plot(ts_airpassengers, xlab = "Year", ylab = "Number of Passengers", main = "Line Chart of Air Passenger Numbers (Time Series)")


댓글 없음:

댓글 쓰기