How to create a normal Q-Q plot in R using ggplot2? | StatswithR | Arnab Hazra

StatswithR
StatswithR
4.6 هزار بار بازدید - 4 سال پیش - Here we explain how to
Here we explain how to generate a presentation/publication-quality normal Q-Q plot in R/R-studio using ggplot2. The codes for the steps explained in the video are as follows. Copy and paste them into R, run them one-by-one and try to understand what each argument is doing.

#datascience #datavisualization #visualization #ggplot2 #tidyverse #qqplot #rstudio #rcoding #normal

Step 0a: If you don't know how to load data into R, simulate a vector X using

X = rnorm(1e4, mean = 175, sd = 10)

Step 0b: if you have not installed the packages ggplot2 and qqplotr, run

install.packages("ggplot2") library(ggplot2)
install.packages("qqplotr") library(qqplotr)

Step 1: ggplot(mapping = aes(sample = X)) + stat_qq_point(size = 2)

Step 2: ggplot(mapping = aes(sample = X)) +
 stat_qq_point(size = 2) +
 xlab("Theoretical Quantiles") + ylab("Sample Quantiles")

Step 3: ggplot(mapping = aes(sample = X)) +
 stat_qq_point(size = 2) +
 stat_qq_line() +
 xlab("Theoretical Quantiles") + ylab("Sample Quantiles")

Step 4: ggplot(mapping = aes(sample = X)) +
 stat_qq_band() +
 stat_qq_point(size = 2) +
 stat_qq_line() +
 xlab("Theoretical Quantiles") + ylab("Sample Quantiles")

Step 5: ggplot(mapping = aes(sample = X)) +
 stat_qq_band() +
 stat_qq_point(size = 2) +
 stat_qq_line() +
 xlab("Theoretical Quantiles") + ylab("Sample Quantiles") +
 ggtitle("Normal Q-Q plot of heights")

Step 6: ggplot(mapping = aes(sample = X)) +
 stat_qq_band() +
 stat_qq_point(size = 2) +
 stat_qq_line() +
 xlab("Theoretical Quantiles") + ylab("Sample Quantiles") +
 ggtitle("Normal Q-Q plot of heights") +
 theme(plot.title = element_text(hjust = 0.5))

Step 7: ggplot(mapping = aes(sample = X)) +
 stat_qq_band() +
 stat_qq_point(size = 2) +
 stat_qq_line() +
 xlab("Theoretical Quantiles") + ylab("Sample Quantiles") +
 ggtitle("Normal Q-Q plot of heights") +
 theme(plot.title = element_text(hjust = 0.5)) +
 theme(axis.text=element_text(size=20),
       axis.title=element_text(size=20),
       plot.title = element_text(size=20))
4 سال پیش در تاریخ 1399/05/20 منتشر شده است.
4,653 بـار بازدید شده
... بیشتر