Reputation: 902
I am now dealing with some data and I want to make a boxplot showing minimum, 2.5, 25, 50, 70, 75, 97.5, and maximum. The boxplot should also have a legend showing lines with different colors to represent each quantile. Is there any way to do this? Thanks for any help.
set.seed(123)
Mydata = sample(x=100:300, size = 500, replace = T)
Mydata = c(Mydata, 1, 500)
boxplot(Mydata)
PS. I have tried the code provided by @thelatemail, but get a totally different figure in RStudio. Any solution to this? Thanks.
Upvotes: 4
Views: 5293
Reputation: 7063
A base R solution: If you only want to change part of the boxplot, here 25%- and 75% quantiles to 0.125, 0.9 quantiles:
set.seed(12345)
x <- rnorm(1000)
bp <- boxplot(x, whisklty=0, staplelty=0, range=1.5, plot=FALSE)
bp$stats[c(2, 4), ] <- quantile(x = x, probs = c(0.05, 0.95))
bxp(bp, whisklty=1, staplelty=1, boxfill = "red")
# To add more from inner to outer, e.g.
bp$stats[c(2, 4), ] <- quantile(x = x, probs = c(0.125, 0.9))
bxp(bp, whisklty=1, staplelty=1, boxfill = "lightgray", add=TRUE)
Looks the same as the original, just the box changed.
Upvotes: 0
Reputation: 93843
Just keep overplotting using bxp
:
set.seed(123)
Mydata = sample(x=100:300, size = 500, replace = T)
Mydata = c(Mydata, 1, 500)
bp <- boxplot(Mydata, range=0, plot=FALSE)
vals <- c(
min=min(Mydata),
quantile(Mydata, c(0.025, 0.25, 0.5, 0.7, 0.75, 0.975)),
max=max(Mydata)
)
bxp(bp, whisklty=0, staplelty=0)
bp$stats[2:4,] <- c(vals[2], Inf, vals[5])
bxp(bp, whisklty=0, staplelty=0, add=TRUE)
bp$stats[2:4,] <- c(vals[2], Inf, vals[7])
bxp(bp, whisklty=1, staplelty=1, add=TRUE)
Upvotes: 3
Reputation: 33782
What you want to do cannot be generated easily using the boxplot framework.
Underlying boxplots in R is the boxplot.stats()
function. Let's run it on your data:
boxplot.stats(Mydata)
$stats
[1] 1 152 204 253 300
$n
[1] 502
$conf
[1] 196.8776 211.1224
$out
[1] 500
You can see that $stats
returns in order: lower whisker, 25% quantile, median, 75% quantile, upper whisker. Compare with quantile
:
quantile(Mydata)
0% 25% 50% 75% 100%
1 152 204 253 500
If you use geom_boxplot()
from ggplot2
, it's possible to redefine the values used for the box. But you can only draw the same five values: they are called ymin
, lower
, middle
, upper
and ymax
.
So for example if you wanted the 2.5% quantile as lower
and the 97.5% quantile as upper
, you could try:
data.frame(x = 1,
y0 = min(Mydata),
y025 = quantile(Mydata, 0.025),
y50 = median(Mydata),
y975 = quantile(Mydata, 0.975),
y100 = max(Mydata)) %>%
ggplot(df, aes(x)) +
geom_boxplot(aes(ymin = y0,
lower = y025,
middle = y50,
upper = y975,
ymax = y100),
stat = "identity")
However, you would want to make it clear (using labels perhaps) that this is not a "standard" boxplot.
Another ggplot2
idea is to use geom_jitter
to plot the data points, then add lines for the desired quantiles using geom_hline
. Something like this:
library(tibble)
library(ggplot2)
Mydataq <- quantile(Mydata, probs = c(0.025, 0.25, 0.5, 0.7, 0.75, 0.975)) %>%
as.data.frame() %>%
setNames("value") %>%
rownames_to_column(var = "quantile")
Mydataq %>%
ggplot() +
geom_hline(aes(yintercept = value, color = quantile)) +
geom_jitter(data = tibble(x = "Mydata", y = Mydata),
aes(x = x, y = y))
Upvotes: 3
Reputation: 32548
Here's an idea. You might have to refine it further.
#Data
P = c(2.5, 25, 50, 70, 75, 97.5)
#Quantiles
b = quantile(x = Mydata, probs = P/100)
#Custom funtion
dp = function(at, y1, y2, width, ...){
polygon(x = c(at - width/2, at + width/2, at + width/2, at - width/2),
y = c(y1, y1, y2, y2), ...)
}
#Parameters
at = 1
width = 0.2
graphics.off()
#Whiskers
plot(x = rep(at, length(Mydata)), y = Mydata, type = "l")
segments(x0 = at - width/2, x1 = at + width/2, y0 = min(Mydata), y1 = min(Mydata))
segments(x0 = at - width/2, x1 = at + width/2, y0 = max(Mydata), y1 = max(Mydata))
#Boxes
sapply(1:ceiling(length(b)/2), function(i) {
dp(at = at, y1 = b[i], y2 = b[length(b) + 1 - i], width = width * i, col = i)
})
#OR
sapply(1:ceiling(length(b)/2), function(i) {
segments(x0 = at, x1 = at, y0 = b[i], y1 = b[length(b) + 1 - i],
lwd = 10 * i, col = i, lend = "butt")
})
Upvotes: 2