rggplot2quantileggpubr# How to emulate QQ plot from ggpubr

There are plenty of threads here about using a QQ plot, but I'm trying to figure out how to hand calculate one and in the process I wanted to emulate the one used in `ggpubr`

, as it looks better than the base R version. I have so far at least seemingly produced a QQ plot in base R using this method:

```
#### Load Libraries ####
library(tidyverse)
library(ggpubr)
#### Organize Data ####
x <- sort(iris$Petal.Length)
rank <- c(1:150)
perc.scale <- scale(rank)
df <- data.frame(x,
rank,
perc.scale)
#### Plot in Base R ####
plot(perc.scale,
x,
xlab = "Theoretical Quantiles",
ylab = "Sample Quantiles",
main = "Normal Q-Q Plot")
```

The result is mostly similar to the base R version, but I haven't figured out the QQ line.

Similarly, I can just use `ggqqplot(x)`

on my data to get this:

But when I try to reproduce it in `ggplot`

with my hand-calculated data:

```
df %>%
ggplot(aes(x=perc.scale,
y=x))+
geom_point()+
geom_smooth(method = "lm",
color = "black",
se = F,
linewidth = .5)+
theme_pubr()+
labs(x="Theoretical",
y="Sample")
```

It still looks totally different:

**My main questions are 1) how do I get the correct regression line and 2) how do I get the standard error area to show up?** I'm also unsure of why the `ggplot`

version looks rotated compared to the `ggpubr`

version, but that isn't as important for now.

Solution

You could use `stat_qq_line`

to get a qq-plot in `ggplot`

like this:

```
#### Load Libraries ####
library(tidyverse)
library(ggpubr)
#### Organize Data ####
x <- sort(iris$Petal.Length)
rank <- c(1:150)
perc.scale <- scale(rank)
df <- data.frame(x,
rank,
perc.scale)
ggplot(df, aes(sample = x)) +
stat_qq() +
stat_qq_line() +
theme_pubr()+
labs(x="Theoretical",
y="Sample")
```

^{Created on 2023-01-29 with reprex v2.0.2}

You could use `qqplotr`

with `stat_qq_band`

to add confidence interval like this:

```
#### Load Libraries ####
library(tidyverse)
library(ggpubr)
library(qqplotr)
ggplot(df, aes(sample = x)) +
stat_qq_band() +
stat_qq_point() +
stat_qq_line() +
theme_pubr()+
labs(x="Theoretical",
y="Sample")
```

^{Created on 2023-01-29 with reprex v2.0.2}

- geom_area stacks areas by default
- running a PCA on a RasterStack in R
- How can I extract a value from a dataframe based on a range of values?
- Confidence Interval for Some (But Not All) Interaction Terms in a Polynomial Interacted with a Binary Treatment
- Avoiding crazy lines while mapping in r
- Overlay points on top of violin plot
- Add percentage labels to geom_col()
- How to place a js inside a swiper with appendTo()?
- How to make a single plot from two dataframes with ggplot2
- Error in installing "TopicModels" package in Google Colab
- Identify connected subnetworks (R-igraph)
- Adding labels to geom_col()
- Legend title in ggplot2
- R list files with multiple conditions
- R - getting count of maximum-sized sub-group when summarising at prior group_by level
- Problems when running GDC_prepare in R
- Filtering files with names starting with a specific string
- Mutate a vector within a pipe chain
- How to sum a variable by group
- Using hex code to change text color in RMarkdown PDF (R)
- How to Remove Degree and Cardinal Direction Symbols from ggplot Coordinate Axes
- rstan and brms cause R and RStudio session abort
- How to change the plot background color generated by plot(effect(...)) in grey with white grid in R？
- SQL query on arrow duckdb workflow in R
- Venn diagram with duplicated elements
- R- Filter by time closest to midnight
- Difference between rlm() and lm_robust
- Is there a way to combine sorting an rhandsontable and removing from an rhandsontable?
- Split violin plot with ggplot2
- ggbarplot top of one bar does not align with its error bar