فصل 12 نمودار

برای رسم نمودار در R توابع زیادی وجود دارد که می‌توانیم برای رسم مدل‌های مختلف نمودار از آن‌ها استفاده کنیم.

Code
x <- c("a" = 20, "b" = 40, "c" = 10, "d" = 30)
  • نمودار دایره‌ای
Code
pie(x)

  • نمودار نواری
Code
barplot(x)

  • نمودار ستونی یا بافت نگاشت
Code
iris$Sepal.Width |> hist()

  • نمودار چگالی
Code
iris$Sepal.Width |> density() |> plot()

  • نمودار پراکندگی
Code
plot(iris$Sepal.Width, iris$Petal.Length)

  • نمودار جعبه‌ای
Code
boxplot(iris$Sepal.Length ~ iris$Species)

  • رسم نمودار کلی از داده‌ها
Code
plot(iris)

  • ذخیره نمودار در فرمت‌های مختلف
Code
# pdf("img/rplot.pdf", width = 350, height = 350)
# png("img/rplot.png", width = 350, height = 350)
jpeg("img/rplot.jpg", width = 350, height = 350)
iris$Sepal.Width |> density() |> plot()
dev.off()
  • ggplot2: یکی از بهترین بسته‌های رسم نمودار، بسته ggplot2 است.
Code
library(ggplot2)

iris |> 
  ggplot() +
  aes(x = Petal.Length, y = Petal.Width, color = Species) +
  geom_point()