Showing posts with label histogram. Show all posts
Showing posts with label histogram. Show all posts

## How to draw the histogram in R

library("ggplot2", lib.loc="~/R/win-library/3.6")
data(mpg)
hist(mpg\$cty)

> hist(mpg\$cty,
+      xlab = "Miles Per Gallon (City)",
+      main = "Histogram of MPG (City)",
+      breaks = 12,
+      col = "dodgerblue",
+      border = "darkorange")

> hist(mpg\$cty,
+      xlab = "Miles Per Gallon (City)",
+      main = "Histogram of MPG (City)",
+      breaks = 12,
+      col = "red",
+      border = "darkorange")

### How to draw bar plot-R

> Barplots
> barplot(table(mpg\$drv))
> barplot(table(mpg\$drv),
+         xlab = "Drivetrain (f = FWD, r = RWD, 4 = 4WD)",
+         ylab = "Frequency",
+         main = "Drivetrains",
+         col = "blue",
+         border = "darkorange")
> Boxplots
> unique(mpg\$drv)
[1] "f" "4" "r"
>
> boxplot(mpg\$hwy)
> boxplot(hwy ~ drv, data = mpg)
> boxplot(hwy ~ drv, data = mpg,
+         xlab = "Drivetrain (f = FWD, r = RWD, 4 = 4WD)",
+         ylab = "Miles Per Gallon (Highway)",
+         main = "MPG (Highway) vs Drivetrain",
+         pch = 18,
+         cex = 2,
+         col = "darkorange",
+         border = "dodgerblue")

#### How to draw Scatterplots

> plot(hwy ~ displ, data = mpg)
> plot(hwy ~ displ, data = mpg,
+      xlab = "Engine Displacement (in Liters)",
+      ylab = "Miles Per Gallon (Highway)",
+      main = "MPG (Highway) vs Engine Displacement",
+      pch = 20,
+      cex = 6,
+      col = "blue")
> hist(mpg\$cty,
+      xlab = "Miles Per Gallon (City)",
+      main = "Histogram of MPG (City)",
+      breaks = 12,
+      col = "dodgerblue",
+      border = "darkorange")